今天開發中遇到複合主鍵的問題,項目趕時間是以我選用了注解的方式來配置實體了,那麼從配置到使用的具體過程如下了
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 對象
因為複合主鍵隻用當所需字段都相同時候才認為是唯一對象。。。。