今天开发中遇到复合主键的问题,项目赶时间所以我选用了注解的方式来配置实体了,那么从配置到使用的具体过程如下了
1. 配置
一般都把复合主键歌令放到一个类中如下:
import java.io.Serializable;
public class WanbuStateMonthPerPK implements Serializable {//主键类 必须实现序列化
private Integer beginTime;// 主键
private Integer userid; // 主键
public WanbuStateMonthPerPK() {// 必须有默认构造
super();
// TODO Auto-generated constructor stub
}
@Override
public boolean equals(Object obj) {// 必须重写equals() 和 hashcode()不过这个可以用myeclipse来生成快捷键 shift+alt+r+h 也可借助于commonclipse插件
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final WanbuStateMonthPerPK other = (WanbuStateMonthPerPK) obj;
if (beginTime == null) {
if (other.beginTime != null)
return false;
} else if (!beginTime.equals(other.beginTime))
return false;
if (userid == null) {
if (other.userid != null)
return false;
} else if (!userid.equals(other.userid))
return false;
return true;
}
public Integer getBeginTime() {
return beginTime;
}
public Integer getUserid() {
return userid;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((beginTime == null) ? 0 : beginTime.hashCode());
result = prime * result + ((userid == null) ? 0 : userid.hashCode());
return result;
}
public void setBeginTime(Integer beginTime) {
this.beginTime = beginTime;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
}
需要映射的实体类
import javax.persistence.*;
@Entity// 该类是持久化类
@Table(name="Wanbu_state_Monthper")// 映射表
@IdClass(WanbuStateMonthPerPK.class)// 使用的主键类
public class WanbuStateMonthper {
@Id
private Integer beginTime; // @id 用来表明与主键类所对应的字段
@Id
private Integer userid;
private Integer updateTime;
private Long monthperSteps;
public WanbuStateMonthper() {
super();
// TODO Auto-generated constructor stub
}
public WanbuStateMonthper(Long monthSteps){
this.monthperSteps=monthSteps;
}
public Integer getBeginTime() {
return beginTime;
}
public Integer getUserid() {
return userid;
}
public void setBeginTime(Integer beginTime) {
this.beginTime = beginTime;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public int getUpdateTime() {
return updateTime;
}
public void setUpdateTime(int updateTime) {
this.updateTime = updateTime;
}
public Long getMonthperSteps() {
return monthperSteps;
}
public void setMonthperSteps(Long monthperSteps) {
this.monthperSteps = monthperSteps;
}
public WanbuStateMonthPerPK getMonthPK() {
return monthPK;
}
public void setMonthPK(WanbuStateMonthPerPK monthPK) {
this.monthPK = monthPK;
}
}
2,配置完成了我们来看看怎么用吧
要用他们必须在hibernate-cfg.xml 中进行映射 <mapping class="完全限定类名" />
不过在用注解时候我们获取SessionFactory 是 只能通过 SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); l来加载实例化hibernate-cfg.xml 了。。
3. 等一切配置成功我们就可以使用他了 下面来看看如何使用它和使用它完成了什么。
先看代码:假设数据库已连接上并且session 已开
WanbuStateMonthPerPK monthpk=new WanbuStateMonthPerPK();
monthpk.setUserid(Integer.parseInt(uid));
monthpk.setBeginTime(super.getIntByDate(f.parse(sRet),"yyyyMMdd"));
WanbuStateMonthPer monthinfo=supper.get(WanbuStateMonthper.class, monthpk); //调用hibernate的get方法
使用前先给主键类赋值然后调用hibernate的get方法即可获取满足于userid=uid and begintime=你传的值的WanbuStateMonthPer 对象
因为复合主键只用当所需字段都相同时候才认为是唯一对象。。。。