2010.01.14——hibernate annotatio复合主键并解决2010.01.13的问题
昨天的问题,我终于找到问题所在了,原因是我的ST_PPTN_R这个pojo类有问题
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="ST_PPTN_R")
public class ST_PPTN_R {
@Id
@Column(length=8)
private String STCD;
@Id
@Column
private Date TM;
@Column(precision=5,scale=1)
private Double DRP;
private Double INTV;
private Double PDR;
@Column(precision=5,scale=1)
private Double DYP;
@Column(length=1)
private String WTH;
public String getSTCD() {
return STCD;
}
public void setSTCD(String stcd) {
STCD = stcd;
}
public Date getTM() {
return TM;
}
public void setTM(Date tm) {
TM = tm;
}
public Double getDRP() {
return DRP;
}
public void setDRP(Double drp) {
DRP = drp;
}
public Double getINTV() {
return INTV;
}
public void setINTV(Double intv) {
INTV = intv;
}
public Double getPDR() {
return PDR;
}
public void setPDR(Double pdr) {
PDR = pdr;
}
public Double getDYP() {
return DYP;
}
public void setDYP(Double dyp) {
DYP = dyp;
}
public String getWTH() {
return WTH;
}
public void setWTH(String wth) {
WTH = wth;
}
public ST_PPTN_R(String STCD,Date TM,Double DYP){
super();
this.STCD = STCD;
this.TM = TM;
if(DYP==null)
DYP = 0.0;
this.DYP = DYP;
}
public ST_PPTN_R(){}
}
STCD和TM应该是联合主键的,而我就写了两个@ID,所以会一直报错说找不到TM的
那么现在就是要用hibernate annotation来设置联合主键了,我在网上看了好多方法,但是都要修改pojo类,而我后面要用到hql的动态实例查询,所以不能修改pojo类,我的解决方法如下:
pojo类:
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@IdClass(STCD_TM.class) //注意:变化在这里
@Entity
@Table(name="ST_PPTN_R")
public class ST_PPTN_R {
@Id
@Column(length=8)
private String STCD;
@Id
@Column
private Date TM;
@Column(precision=5,scale=1)
private Double DRP;
private Double INTV;
private Double PDR;
@Column(precision=5,scale=1)
private Double DYP;
@Column(length=1)
private String WTH;
public String getSTCD() {
return STCD;
}
public void setSTCD(String stcd) {
STCD = stcd;
}
public Date getTM() {
return TM;
}
public void setTM(Date tm) {
TM = tm;
}
public Double getDRP() {
return DRP;
}
public void setDRP(Double drp) {
DRP = drp;
}
public Double getINTV() {
return INTV;
}
public void setINTV(Double intv) {
INTV = intv;
}
public Double getPDR() {
return PDR;
}
public void setPDR(Double pdr) {
PDR = pdr;
}
public Double getDYP() {
return DYP;
}
public void setDYP(Double dyp) {
DYP = dyp;
}
public String getWTH() {
return WTH;
}
public void setWTH(String wth) {
WTH = wth;
}
public ST_PPTN_R(String STCD,Date TM,Double DYP){
super();
this.STCD = STCD;
this.TM = TM;
if(DYP==null)
DYP = 0.0;
this.DYP = DYP;
}
public ST_PPTN_R(){}
}
复合主键类:
import java.io.Serializable;
import java.util.Date;
public class STCD_TM implements Serializable{
private String STCD;
private Date TM;
public String getSTCD() {
return STCD;
}
public void setSTCD(String stcd) {
STCD = stcd;
}
public Date getTM() {
return TM;
}
public void setTM(Date tm) {
TM = tm;
}
public int hashCode(){
return (int) STCD.hashCode();
}
public boolean equals(Object obj){
if (obj == this) return true;
if (!(obj instanceof STCD_TM)) return false;
if (obj == null) return false;
STCD_TM st = (STCD_TM) obj;
return st.TM == TM && st.STCD.equals(STCD);
}
}
这里这个复合主键类有几点要注意的;
1.类里面的字段或属性的类型和名称必须与使用 @Id 进行批注的实体主键字段或属性的类型和名称相对应
2.它必须为 public,并且必须有一个 public 无参数构造函数。
3.它必须是可序列化的。
4.它必须定义 equals 和 hashCode 方法。