JPA渴望获取不返回任何结果

时间:2016-10-05 02:54:15

标签: hibernate jpa

我经历了几个帖子,但没有一个对我有用。我试图同时获取Cnsm记录及其关联的cnsmPrefCntcInfos子记录。我还能够看到生成的hibernate查询来检索子(cnsmPrefCntcInfos)记录。但是,当我打印CNSM时,我会找回一个空对象。

@Entity
@Table(name = "CNSM" )
public class Cnsm implements java.io.Serializable {

private BigDecimal cnsmId;
private String cnsmFstNm;
private String cnsmMidlNm;

@Id

@Column(name = "CNSM_ID", unique = true, nullable = false, precision = 22,    scale = 0)
public BigDecimal getCnsmId() {
    return this.cnsmId;
}

public void setCnsmId(BigDecimal cnsmId) {
    this.cnsmId = cnsmId;
}

@Column(name = "CNSM_FST_NM", length = 35)
public String getCnsmFstNm() {
    return this.cnsmFstNm;
}

public void setCnsmFstNm(String cnsmFstNm) {
    this.cnsmFstNm = cnsmFstNm;
}

@Column(name = "CNSM_MIDL_NM", length = 25)
public String getCnsmMidlNm() {
    return this.cnsmMidlNm;
}

public void setCnsmMidlNm(String cnsmMidlNm) {
    this.cnsmMidlNm = cnsmMidlNm;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "cnsm")
private Set<CnsmPrefCntcInfo> cnsmPrefCntcInfos = new HashSet<CnsmPrefCntcInfo>(0);



public Set<CnsmPrefCntcInfo> getCnsmPrefCntcInfos() {
    return this.cnsmPrefCntcInfos;
}

public void setCnsmPrefCntcInfos(Set<CnsmPrefCntcInfo> cnsmPrefCntcInfos) {
    this.cnsmPrefCntcInfos = cnsmPrefCntcInfos;
}

}

@Entity
@Table(name = "CNSM_PREF_CNTC_INFO")
public class CnsmPrefCntcInfo implements java.io.Serializable {



private BigDecimal cnsmPrefCntcInfoId;
private Cnsm cnsm;
private String cntcTyp;

private Set<CnsmPrefPstAdr> cnsmPrefPstAdrs = new HashSet<CnsmPrefPstAdr>(0);
private Set<CnsmPrefTelCmnct> cnsmPrefTelCmncts = new HashSet<CnsmPrefTelCmnct>(0);





@Id

@Column(name = "CNSM_PREF_CNTC_INFO_ID", unique = true, nullable = false, precision = 22, scale = 0)
public BigDecimal getCnsmPrefCntcInfoId() {
    return this.cnsmPrefCntcInfoId;
}

public void setCnsmPrefCntcInfoId(BigDecimal cnsmPrefCntcInfoId) {
    this.cnsmPrefCntcInfoId = cnsmPrefCntcInfoId;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CNSM_ID")

public Cnsm getCnsm() {
    return this.cnsm;
}

public void setCnsm(Cnsm cnsm) {
    this.cnsm = cnsm;
}

@Column(name = "CNTC_TYP", length = 50)
public String getCntcTyp() {
    return this.cntcTyp;
}

public void setCntcTyp(String cntcTyp) {
    this.cntcTyp = cntcTyp;
}



@OneToMany(fetch = FetchType.LAZY, mappedBy = "cnsmPrefCntcInfo")
public Set<CnsmPrefPstAdr> getCnsmPrefPstAdrs() {
    return this.cnsmPrefPstAdrs;
}

public void setCnsmPrefPstAdrs(Set<CnsmPrefPstAdr> cnsmPrefPstAdrs) {
    this.cnsmPrefPstAdrs = cnsmPrefPstAdrs;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "cnsmPrefCntcInfo")
public Set<CnsmPrefTelCmnct> getCnsmPrefTelCmncts() {
    return this.cnsmPrefTelCmncts;
}

public void setCnsmPrefTelCmncts(Set<CnsmPrefTelCmnct> cnsmPrefTelCmncts) {
    this.cnsmPrefTelCmncts = cnsmPrefTelCmncts;
}

}

回购服务

@Query("SELECT c FROM Cnsm c WHERE c.id = '123'")

Cnsm findCnsmById();

主要课程

  Cnsm  c = IRepository.findCnsmById();
  System.out.print(c); // returns empty cnsmPrefCntcInfos object

Hibernate:选择cnsm0_.CNSM_ID作为CNSM_ID1_4_,cnsm0_.CNSM_FST_NM作为CNSM_FST4_4_,cnsm0_.CNSM_MIDL_NM作为来自CNSM cnsm0_的CNSM_MID6_4_,其中cnsm0_.CNSM_ID = 103

下面是为获取cnsmPrefCntcInfos而生成的Hibernate查询,但不确定为什么cnsmPrefCntcInfos返回为空

休眠:?从CNSM_PREF_CNTC_INFO cnsmprefcn0_选择cnsmprefcn0_.CNSM_ID如CNSM_ID15_4_1_,cnsmprefcn0_.CNSM_PREF_CNTC_INFO_ID如CNSM_PRE1_7_1_,cnsmprefcn0_.CNSM_PREF_CNTC_INFO_ID如CNSM_PRE1_7_0_,cnsmprefcn0_.CNSM_ID如CNSM_ID15_7_0_,cnsmprefcn0_.CNTC_TYP如CNTC_TYP2_7_0_其中cnsmprefcn0_.CNSM_ID =

如何在检索cnsm对象时获取cnsmPrefCntcInfos(EAGER)?我在cnsm中有许多像cnsmPrefCntcInfos这样的关联,我想在获取cnsm时检索它。

1 个答案:

答案 0 :(得分:0)

在您的Cnsm中你需要将id的列名更改为&#34; CNSM_ID&#34;,从我看到的Cnsm fetch是懒惰不急切。