博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JPA联合主键@EmbeddedId使用详解附查询例子
阅读量:5952 次
发布时间:2019-06-19

本文共 5163 字,大约阅读时间需要 17 分钟。

花了2个小时的时间解决这个问题,网上资料太少,记录下
 
 
详情看源文件TBicPrmCompute,TBicPrmComputePK
package com.isoftstone.core.domain;import java.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 implements Serializable {    private static final long serialVersionUID = 1L;    private TBicPrmComputePK id;    private String cBsnsTyp;    private String cCmpnyAgtCde;    private String cSlsCde;    private String cYl1;    private String cYl2;    private Double nMaxInsrntTm;    private Double nMinInsrntTm;    private Double nRate;    private Double nTotalAmt;    public TBicPrmCompute() {    }    @EmbeddedId    public TBicPrmComputePK getId() {        return this.id;    }    public void setId(TBicPrmComputePK id) {        this.id = id;    }        @Column(name="C_BSNS_TYP")    public String getCBsnsTyp() {        return this.cBsnsTyp;    }    public void setCBsnsTyp(String cBsnsTyp) {        this.cBsnsTyp = cBsnsTyp;    }    @Column(name="C_CMPNY_AGT_CDE")    public String getCCmpnyAgtCde() {        return this.cCmpnyAgtCde;    }    public void setCCmpnyAgtCde(String cCmpnyAgtCde) {        this.cCmpnyAgtCde = cCmpnyAgtCde;    }    @Column(name="C_SLS_CDE")    public String getCSlsCde() {        return this.cSlsCde;    }    public void setCSlsCde(String cSlsCde) {        this.cSlsCde = cSlsCde;    }    @Column(name="C_YL1")    public String getCYl1() {        return this.cYl1;    }    public void setCYl1(String cYl1) {        this.cYl1 = cYl1;    }    @Column(name="C_YL2")    public String getCYl2() {        return this.cYl2;    }    public void setCYl2(String cYl2) {        this.cYl2 = cYl2;    }    @Column(name="N_MAX_INSRNT_TM")    public Double getNMaxInsrntTm() {        return this.nMaxInsrntTm;    }    public void setNMaxInsrntTm(Double nMaxInsrntTm) {        this.nMaxInsrntTm = nMaxInsrntTm;    }    @Column(name="N_MIN_INSRNT_TM")    public Double getNMinInsrntTm() {        return this.nMinInsrntTm;    }    public void setNMinInsrntTm(Double nMinInsrntTm) {        this.nMinInsrntTm = nMinInsrntTm;    }    @Column(name="N_RATE")    public Double getNRate() {        return this.nRate;    }    public void setNRate(Double nRate) {        this.nRate = nRate;    }    @Column(name="N_TOTAL_AMT")    public Double getNTotalAmt() {        return this.nTotalAmt;    }    public void setNTotalAmt(Double nTotalAmt) {        this.nTotalAmt = nTotalAmt;    }}
package com.isoftstone.core.domain;import java.io.Serializable;import javax.persistence.*;/** * The primary key class for the T_BIC_PRM_COMPUTE database table. *  */@Embeddablepublic class TBicPrmComputePK implements Serializable {    //default serial version id, required for serializable classes.    private static final long serialVersionUID = 1L;    private String cProdNo;    private String cBisCode;    private String cBicNo;    public TBicPrmComputePK() {    }    @Column(name="C_PROD_NO")    public String getCProdNo() {        return this.cProdNo;    }    public void setCProdNo(String cProdNo) {        this.cProdNo = cProdNo;    }    @Column(name="C_BIS_CODE")    public String getCBisCode() {        return this.cBisCode;    }    public void setCBisCode(String cBisCode) {        this.cBisCode = cBisCode;    }    @Column(name="C_BIC_NO")    public String getCBicNo() {        return this.cBicNo;    }    public void setCBicNo(String cBicNo) {        this.cBicNo = cBicNo;    }    public boolean equals(Object other) {        if (this == other) {            return true;        }        if (!(other instanceof TBicPrmComputePK)) {            return false;        }        TBicPrmComputePK castOther = (TBicPrmComputePK)other;        return             this.cProdNo.equals(castOther.cProdNo)            && this.cBisCode.equals(castOther.cBisCode)            && this.cBicNo.equals(castOther.cBicNo);    }        public int hashCode() {        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();                return hash;    }}

 

 
关键是查询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

转载地址:http://ziaxx.baihongyu.com/

你可能感兴趣的文章
Spring 文件上传功能
查看>>
RAC静默安装与DG搭建
查看>>
windows 下mysql的安装于使用(启动、关闭)
查看>>
Android 中文 API (28) —— CheckedTextView
查看>>
PHPStorm IDE 快捷键(MAC)
查看>>
反编译代码遇到的问题
查看>>
Android Bitmaps缓存
查看>>
learn go ifelse
查看>>
LINUX中常用操作命令
查看>>
自定义异常类一
查看>>
Launch和Shut Off操作详解 - 每天5分钟玩转 OpenStack(30)
查看>>
23.3. 操作系统监控需求
查看>>
美国国家标准技术局发布应用容器安全指南
查看>>
webservice远程调试开启
查看>>
WinForm员工信息表
查看>>
【AIX】AIX 开机自动挂载NFS共享
查看>>
[20150123]热链竞争.txt
查看>>
【翻译+整理】.NET Core的介绍
查看>>
[20150508]列顺序问题.txt
查看>>
Aliware研究开篇
查看>>