Hibernate Child Collection Limited When Using Left Join in Criteria

人盡茶涼 提交于 2019-12-04 11:09:30

This problem is described here and seems to be fixed in Hibernate 3.6

https://hibernate.onjira.com//browse/HHH-2049

I've tried this out: when executing this query and afterwards getting calling parent.getChildren() then:

  • LEFT JOIN: one query is executed containing the parent and the one matching child, no subsequent query is executed when calling getChildren()
  • INNER_JOIN: 2 queries are executed: one to find the parents with matching childs and another when calling getChildren()

So it seems that when calling LEFT_JOIN the children (in this case the matching children) are EAGERLY fetched and the children collection of the Parent is already populated. For the INNER_JOIN however this collection is marked as a proxy and is initialized when calling getChildren(); this second query will of course no longer take the restriction on name into account and will simply fetch all children for the Parent.

This does seem to happen 'inside' hibernate, meaning the join type will affect how hibernate treats the results. Although the generated SQL between a left and inner join differs slightly (in my test the parent.id and child.id were twice in the select clause) the results returned when running the SQL in a DB browser are the same.

I'm not experienced enough to determine if this a bug or not but it doesn't feel like one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!