java联合主键的使用_JPA联合主键@EmbeddedId使用详解附查询例子

花了2个小时的时间解决这个问题,网上资料太少,记录下

详情看源文件TBicPrmCompute,TBicPrmComputePK

packagecom.isoftstone.core.domain;importjava.io.Serializable;import javax.persistence.*;/*** The persistent class for the T_BIC_PRM_COMPUTE database table.

**/@Entity

@NamedQueries( {

@NamedQuery(name= "findPrmComputeById", query = "select d from TBicPrmCompute d where d.id.CBisCode = ?1 and d.id.CBicNo = ?2 and d.id.CProdNo = ?3")

} )

@Table(name="T_BIC_PRM_COMPUTE")public class TBicPrmCompute implementsSerializable {private static final long serialVersionUID = 1L;privateTBicPrmComputePK id;privateString cBsnsTyp;privateString cCmpnyAgtCde;privateString cSlsCde;privateString cYl1;privateString cYl2;privateDouble nMaxInsrntTm;privateDouble nMinInsrntTm;privateDouble nRate;privateDouble nTotalAmt;publicTBicPrmCompute() {

}

@EmbeddedIdpublicTBicPrmComputePK getId() {return this.id;

}public voidsetId(TBicPrmComputePK id) {this.id =id;

}

@Column(name="C_BSNS_TYP")publicString getCBsnsTyp() {return this.cBsnsTyp;

}public voidsetCBsnsTyp(String cBsnsTyp) {this.cBsnsTyp =cBsnsTyp;

}

@Column(name="C_CMPNY_AGT_CDE")publicString getCCmpnyAgtCde() {return this.cCmpnyAgtCde;

}public voidsetCCmpnyAgtCde(String cCmpnyAgtCde) {this.cCmpnyAgtCde =cCmpnyAgtCde;

}

@Column(name="C_SLS_CDE")publicString getCSlsCde() {return this.cSlsCde;

}public voidsetCSlsCde(String cSlsCde) {this.cSlsCde =cSlsCde;

}

@Column(name="C_YL1")publicString getCYl1() {return this.cYl1;

}public voidsetCYl1(String cYl1) {this.cYl1 =cYl1;

}

@Column(name="C_YL2")publicString getCYl2() {return this.cYl2;

}public voidsetCYl2(String cYl2) {this.cYl2 =cYl2;

}

@Column(name="N_MAX_INSRNT_TM")publicDouble getNMaxInsrntTm() {return this.nMaxInsrntTm;

}public voidsetNMaxInsrntTm(Double nMaxInsrntTm) {this.nMaxInsrntTm =nMaxInsrntTm;

}

@Column(name="N_MIN_INSRNT_TM")publicDouble getNMinInsrntTm() {return this.nMinInsrntTm;

}public voidsetNMinInsrntTm(Double nMinInsrntTm) {this.nMinInsrntTm =nMinInsrntTm;

}

@Column(name="N_RATE")publicDouble getNRate() {return this.nRate;

}public voidsetNRate(Double nRate) {this.nRate =nRate;

}

@Column(name="N_TOTAL_AMT")publicDouble getNTotalAmt() {return this.nTotalAmt;

}public voidsetNTotalAmt(Double nTotalAmt) {this.nTotalAmt =nTotalAmt;

}

}

packagecom.isoftstone.core.domain;importjava.io.Serializable;import javax.persistence.*;/*** The primary key class for the T_BIC_PRM_COMPUTE database table.

**/@Embeddablepublic class TBicPrmComputePK implementsSerializable {//default serial version id, required for serializable classes.

private static final long serialVersionUID = 1L;privateString cProdNo;privateString cBisCode;privateString cBicNo;publicTBicPrmComputePK() {

}

@Column(name="C_PROD_NO")publicString getCProdNo() {return this.cProdNo;

}public voidsetCProdNo(String cProdNo) {this.cProdNo =cProdNo;

}

@Column(name="C_BIS_CODE")publicString getCBisCode() {return this.cBisCode;

}public voidsetCBisCode(String cBisCode) {this.cBisCode =cBisCode;

}

@Column(name="C_BIC_NO")publicString getCBicNo() {return this.cBicNo;

}public voidsetCBicNo(String cBicNo) {this.cBicNo =cBicNo;

}public booleanequals(Object other) {if (this ==other) {return true;

}if (!(other instanceofTBicPrmComputePK)) {return false;

}

TBicPrmComputePK castOther=(TBicPrmComputePK)other;return

this.cProdNo.equals(castOther.cProdNo)&& this.cBisCode.equals(castOther.cBisCode)&& this.cBicNo.equals(castOther.cBicNo);

}public inthashCode() {final int prime = 31;int hash = 17;

hash= hash * prime + this.cProdNo.hashCode();

hash= hash * prime + this.cBisCode.hashCode();

hash= hash * prime + this.cBicNo.hashCode();returnhash;

}

}

关键是查询JPQL的写法费了较久时间

@NamedQueries( {

@NamedQuery(name = "findPrmComputeById", query = "select d from TBicPrmCompute d where d.id.CBisCode = ?1 and d.id.CBicNo = ?2 and d.id.CProdNo = ?3")

} )

DAOImpl实现类

@Transactional

@Override

public TBicPrmCompute findPrmComputeById(String cBisCode, String cBicNo, String cProdNo)

throws DataAccessException {

Query query = createNamedQuery("findPrmComputeById", -1, -1, cBisCode, cBicNo, cProdNo);

return (TBicPrmCompute) query.getSingleResult();

}

调用接口

TBicPrmCompute tBicPrmCompute = prmComputeDAO.findPrmComputeById(cBisCode, cBicNo, cProdNo);

需要注意的是

@EmbeddedId

public TBicPrmComputePK getId() {

return this.id;

}

一般写JPQL的属性是from EntityA d where d.PropertyGetName

但是由于联合主键对象需要用来作JPQL的条件,看上面的联合主键ID,比如想拿ID中的A属性作条件,写法是d.id.A=?1

这里不是get方法的名字呢,直接是属性名字后再是get方法名字getA()去掉get之后的A

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值