主键类:定义为@Embeddable
@Embeddable
public F {
private id;
private id2;
public getId {
id;
}
public void Id( id) {
this.id = id;
}
public getId2 {
id2;
}
public void Id2( id2) {
this.id2 = id2;
}
}
持久化类:
@Entity
@Table(name="E_USER",uniqueConstras={
@UniqueConstra(columnNames={"yahoo"})
})
@IdClass(F.)//将F作为主键类
public User {
private id;
private id2;
//id id2必须和F中属性致
private String yahoo;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public getId {
id;
}
public void Id( id) {
this.id = id;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public getId2 {
id2;
}
public void Id2( id2) {
this.id2 = id2;
}
public String getYahoo {
yahoo;
}
public void Yahoo(String yahoo) {
this.yahoo = yahoo;
}
}
最终生成sql:(mysql)
create table E_USER (id eger not null, id2 eger not null, yahoo varchar(255), primary key (id, id2), unique (yahoo))
最新评论