当前位置:天才代写 > tutorial > JAVA 教程 > hibernate annoation (六 复合主键)

hibernate annoation (六 复合主键)

2017-11-12 08:00 星期日 所属: JAVA 教程 浏览:440

主键类:界说为@Embeddable

@Embeddable
public class F {
 private int id;
 private int id2;
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public int getId2() {
 return id2;
 }
 public void setId2(int id2) {
 this.id2 = id2;
 }
 
}

耐久化类:

@Entity 
@Table(name="E_USER",uniqueConstraints={
 @UniqueConstraint(columnNames={"yahoo"})
 })
@IdClass(F.class)//将F作为主键类 
public class User {
 private int id;
 private int id2;
//id id2必需和F中的属性一致 
 private String yahoo;
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 public int getId2() {
 return id2;
 }
 public void setId2(int id2) {
 this.id2 = id2;
 }
 public String getYahoo() {
 return yahoo;
 }
 public void setYahoo(String yahoo) {
 this.yahoo = yahoo;
 }
 
}

最终生成sql:(mysql)

create table E_USER (id integer not null, id2 integer not null, yahoo varchar(255), primary key (id, id2), unique (yahoo))

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式