1
我使用彈簧數據與JPQL分組選擇,一切工作和統計數字計數,但是當我想訪問競爭對象我' m如果這個錯誤:如何通過彈簧數據(jpql),通過下一個其他對象的關係來獲取對象
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
我有這些實體,我想收集統計
@Entity
public class BetCourse {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MATCH_ID", nullable = false)
private BetMatch betMatch;
}
@Entity
public class BetMatch {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "betMatch")
private List<BetCourse> courses = new ArrayList<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COMPETITION_ID", nullable = false)
private Competition competition;
}
@Entity
public class Competition {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "competition")
private List<BetMatch> betMatches = new ArrayList<>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "competition")
private List<Stats> stats = new ArrayList<>();
}
而且我挖從這些類數據的統計類
@Entity
public class Stats {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COMPETITION_ID", nullable = false)
private Competition competition;
}
通過此選擇:
@Query("select new Stats(" +
"c.course, " +
"c.courseType, " +
"count(*), " +
"SUM(CASE WHEN win = true THEN 1 ELSE 0 END), " +
"c.betMatch.competition) " +
"from BetCourse c " +
group by c.course, c.betMatch.competition, c.courseType")
public List<Stats> computeStatsByBetShop();
,當我查看在大賽獲獎對象
stat.getCompetition()
我怎樣才能獲取該對象?有可能以某種方式與聯合提取相結合嗎?
我得到這個,當我使用此代碼:org.hibernate.QueryException:通過查詢引起指定連接抓取,但獲取協會的老闆是不存在於選擇列表[ FromElement {顯式,而不是集合連接,獲取連接,獲取非懶惰屬性,classAlias = bm,role = com.patientandrich.model.database.BetCourse.betMatch,tableName = bet_match,tableAlias = betmatch1_,origin = bet_course betcourse0_,columns = {betcourse0_.match_id,className = com.patientandrich.model.database.BetMatch}}] – user1604064
哎呀,我想我已經放了一個額外的聯合抓取。嘗試刪除最後一個,只是讓「join fetch c.betMatch bm」。我編輯了我的答案。如果它仍然不起作用,我會嘗試在這裏創建一個具有相同結構的最小項目,並查看它爲什麼不起作用。但是,你可以先嚐試一下這個小修補嗎? –
我試過了,並且出現同樣的錯誤:/當我想要獲取多對一關係時不應該是不同的東西? – user1604064