hibernate annoation (六 复合主键)
2009-09-18 00:00:00 来源:WEB开发网主键类:定义为@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))
- ››Hibernate高级应用:性能优化策略
- ››hibernate 多对多关系详解(包括中间表,一对多字表...
- ››Hibernate实现mysql数据库limit查询方法
- ››Hibernate 之父:是时候升级到Java EE 6了
- ››Hibernate查询
- ››Hibernate和iBATIS比较(摘自网络)
- ››Hibernate使用Projections进行聚合操作
- ››hibernate中java.util.Date类型映射
- ››hibernate中update与saveOrUpdate的区别
- ››Hibernate各种映射关系总结
- ››Hibernate过滤器使用窍门
- ››Hibernate属性查询简单讲述
赞助商链接