Hibernate HQL對關聯關系過濾

如果Parent和Child是一對多關系
查詢Parent時,想過濾parent.children的話,
如果只是單純的寫

select p from Parent p left join p.children child where child.foo='bar'

這樣只是為查詢Parent增加了過濾條件,但當調用查詢結果的p.getChildren()時,扔會查出全部的Child,而不是經過child.foo='bar'過濾過的Child。

如果想要過濾p.children的話,應該使用fetch
select p from Parent p left joinfetchp.children child where child.foo='bar'
在查詢Parent的同時取得children,這樣在調用p.getChildren()時就是過濾后的結果了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容