我正在开发一个应用程序,我必须加入 3-4 个表以进行选择操作。我是带有注释的休眠 3,并在下面添加了我的类结构。但是我尝试在 Join fetch 策略中使用许多变体,但无法在2 次选择中获得结果。请通过并尝试帮助我。 考生班
@JsonAutoDetect
@Entity
@Table(name="HC_RESUME_BANK")
public class Candidates{
private int id;
private String firstName;
private String lastName;
private String email;
private String phone;
@JsonUnwrapped
private Set<CandidateStatus> candidateStats = new HashSet<CandidateStatus>(0);
@OneToMany //(mappedBy="candidate")
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ResID",nullable=false,referencedColumnName="RID")
@JsonIgnore
public Set<CandidateStatus> getCandidateStats() {
return candidateStats;
}
public void setCandidateStats(Set<CandidateStatus> candidateStats) {
this.candidateStats = candidateStats;
}
候选人状态类
@JsonAutoDetect
@Entity
@Table(name="HC_REQ_RESUME")
public class CandidateStatus{
private int id;
private String statusTitle;
@JsonUnwrapped
private Candidates candidate;
private int reqid;
private int resid;
@JsonUnwrapped
private Requirements requirement;
@ManyToOne
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ReqID",insertable=false,updatable=false)
//@JsonIgnore
public Requirements getRequirement() {
return requirement;
}
@ManyToOne //(fetch = FetchType.LAZY,cascade=CascadeType.ALL,targetEntity=Candidates.class)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ResID",insertable=false,updatable=false)
//@ForeignKey(name = "FK_ResID")
public Candidates getCandidate() {
return candidate;
}
public void setCandidate(Candidates candidate) {
this.candidate = candidate;
}
客户类
@JsonAutoDetect
@Entity
@Table(name="HC_CLIENTS")
public class Clients{
private int id;
private String clientName;
@JsonUnwrapped
private Set<Requirements> require= new HashSet<Requirements>(0);
//private List<Requirements> require;
@OneToMany //(fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ClientID",referencedColumnName="RID")
@JsonIgnore
public Set<Requirements> getRequire() {
return require;
}
public void setRequire(Set<Requirements> require) {
this.require = require;
}
最后是需求类
@JsonAutoDetect
@Entity
@Table(name="HC_REQUISITIONS")
public class Requirements{
private int id;
private int clientId;
private String reqTitle;
@JsonUnwrapped
private Clients client;
@ManyToOne
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ClientID",insertable=false,updatable=false)
//@JsonIgnore
public Clients getClient() {
return client;
}
public void setClient(Clients client) {
this.client = client;
}
@OneToMany
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ReqID",referencedColumnName="RID")
@JsonIgnore
public Set<CandidateStatus> getCandidateStatus() {
return candidateStatus;
}
public void setCandidateStatus(Set<CandidateStatus> candidateStatus) {
this.candidateStatus = candidateStatus;
}
我已经消除了避免不必要代码的Getter/Setter方法。
由于发生 N+1 的上述类关联有什么问题?
查询是:
return hibernateTemplate.find("from Requirements as req order by req.reqTitle");
我只想获取Requirement 类的对象,但我得到了所有对象和 N+1 个查询结果
谢谢你的时间
萨钦
@RaulGogo:尝试了条件查询,但问题仍然存在。这些是查询的日志实例。我添加了@BatchSize(size=100),它通过批量获取减少了查询的数量。任何我错过的事情注释配置?
Hibernate:
/* criteria query */ select
this_.RID as RID3_3_,
this_.ClientID as ClientID3_3_,
this_.ReqTitle as ReqTitle3_3_,
candidates2_.ReqID as ReqID3_5_,
candidates2_.RID as RID5_,
candidates2_.RID as RID1_0_,
candidates2_.ResID as ResID1_0_,
candidates2_.reqid as reqid1_0_,
candidates2_.ReqID as ReqID1_0_,
candidates2_.resid as resid1_0_,
candidates2_.StatusTitle as StatusTi4_1_0_,
candidates3_.RID as RID0_1_,
candidates3_.EmailID as EmailID0_1_,
candidates3_.FirstName as FirstName0_1_,
candidates3_.LastName as LastName0_1_,
candidates3_.Mobile as Mobile0_1_,
clients4_.RID as RID2_2_,
clients4_.ClientName as ClientName2_2_
from
HC_REQUISITIONS this_
left outer join
HC_REQ_RESUME candidates2_
on this_.RID=candidates2_.ReqID
left outer join
HC_RESUME_BANK candidates3_
on candidates2_.ResID=candidates3_.RID
left outer join
HC_CLIENTS clients4_
on this_.ClientID=clients4_.RID
order by
this_.ReqTitle asc
Hibernate:
/* load one-to-many com.options.model.Candidates.candidateStats */ select
candidates0_.ResID as ResID0_3_,
candidates0_.RID as RID3_,
candidates0_.RID as RID1_2_,
candidates0_.ResID as ResID1_2_,
candidates0_.reqid as reqid1_2_,
candidates0_.ReqID as ReqID1_2_,
candidates0_.resid as resid1_2_,
candidates0_.StatusTitle as StatusTi4_1_2_,
requiremen1_.RID as RID3_0_,
requiremen1_.ClientID as ClientID3_0_,
requiremen1_.ReqTitle as ReqTitle3_0_,
clients2_.RID as RID2_1_,
clients2_.ClientName as ClientName2_1_
from
HC_REQ_RESUME candidates0_
left outer join
HC_REQUISITIONS requiremen1_
on candidates0_.ReqID=requiremen1_.RID
left outer join
HC_CLIENTS clients2_
on requiremen1_.ClientID=clients2_.RID
where
candidates0_.ResID=?