3 回答

TA貢獻1815條經驗 獲得超10個贊
無法在查詢條件中過濾相關集合。您可以通過在 TechnicalStatus 上進行選擇來獲得它: select ts from TechnicStatus ts join Technic t where ...
我注意到的另一件事:
添加新狀態時,您正在覆蓋現有狀態列表:
public void setTechnicStatus(TechnicStatus technicStatus) {
this.technicStatusList = new ArrayList<>();
this.technicStatusList.add(technicStatus);
}
technicStatusList在字段聲明中初始化。技術中的添加方法:
public void addTechnicStatus(TechnicStatus technicStatus) {
getTechnicStatusList().add(technicStatus);
technicStatus.setTechnic(this);
}
我注意到的另一件事:
使用 join 時不要使用on t.id = ts.technic.id. JPA 將在您編寫時創建正確的本機 SQL:join TechnicStatus ts WHERE ...

TA貢獻1891條經驗 獲得超3個贊
使用 join fetch 應該可以解決這個問題,這將迫使查詢一次急切地運行,并且只帶回與 where 子句匹配的記錄。所以你的查詢是:
TypedQuery<Technic> query = em.createQuery("Select t from Technic t join fetch TechnicStatus ts where t.isDel=false and ts.isActive=true and t.farm.id=:farmId order by t.techGroup.name, t.techType.name, t.techMark.name", Technic.class);

TA貢獻1816條經驗 獲得超4個贊
與技術關聯的技術狀態列表將始終是您的映射定義的完整列表。
基本上你有2個選擇。如果您只對狀態為 Active 的 TechnicalStatus 感興趣,那么您可以在關聯上使用不可移植的 Hibernate 特定@Where子句。
@JsonManagedReference
@OneToMany(mappedBy = "technic")
@Where("active = 1")
private List<TechnicStatus> technicStatusList = new ArrayList<>();
https://dzone.com/articles/hibernate-where-clause
否則,您所做的只是從查詢方法返回一個技術狀態列表,這不是您想要的,而是您所擁有的。
添加回答
舉報