美文网首页
Hibernate HQL对关联关系过滤

Hibernate HQL对关联关系过滤

作者: 枝头残月野狼嚎嗷嗷呜 | 来源:发表于2017-02-13 18:03 被阅读0次

如果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()时就是过滤后的结果了

相关文章

网友评论

      本文标题:Hibernate HQL对关联关系过滤

      本文链接:https://www.haomeiwen.com/subject/pbdtwttx.html