如果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 join
fetch
p.children child where child.foo='bar'
在查詢Parent的同時取得children,這樣在調用p.getChildren()時就是過濾后的結果了