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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容