在标准中使用左连接时的Hibernate Child Collection Limited [英] Hibernate Child Collection Limited When Using Left Join in Criteria

查看:105
本文介绍了在标准中使用左连接时的Hibernate Child Collection Limited的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,让Parent类有一个单独的属性,使用以下数据与类Child进行多对多的关系:

 

| id |名称|
| 1 |家长1 |

儿童
| id | parent_id |名称|
| 1 | 1 | Child1 |
| 2 | 1 | Child2 |

使用以下hibernate条件将返回1父行,并在返回的两行中访问子集合结果:

  session.createCriteria(Parent.class)
.createCriteria('child',CriteriaSpecification.INNER_JOIN)
.add( Restrictions.eq('name','Child1'))
.list()

但是,使用左连接更改上述代码时,会返回1个父行,但在访问子集时只会返回匹配的子行。

  session.createCriteria(Parent.class)
.createCriteria('child',CriteriaSpecification.LEFT_JOIN)
.add(Restrictions.eq('name','Child1'))
.list()

为什么会出现这种副作用?根据您的预期结果,我发现了一些关于使用或避免这种副作用的讨论,但没有说明为什么首先出现这种副作用以及它是否是有意的。最直接的问题是旧的缺陷( http://opensource.atlassian.com / projects / hibernate / browse / HHH-3872 )。


  • 编辑3/24:修正数据*

解决方案

这个问题在这里描述,似乎在Hibernate 3.6中得到了解决。

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


When using hibernate criteria just altering the join type affects the results of the child collections of the root domain class.

For instance, having class Parent have a one-to-many relationship with class Child with the following data:

Parent 
| id | Name     |
|  1 | Parent 1 |

Child
| id | parent_id | Name   |
|  1 |         1 | Child1 |
|  2 |         1 | Child2 |

Using the following hibernate criteria returns the 1 parent row, and accessing the child collection results in the two rows being returned:

session.createCriteria(Parent.class)
    .createCriteria('child', CriteriaSpecification.INNER_JOIN)
    .add( Restrictions.eq( 'name', 'Child1' ) )
    .list()

However, when changing the above code with a left join, the 1 parent row is returned, but only the matching child row is returned when accessing the child collection.

session.createCriteria(Parent.class)
    .createCriteria('child', CriteriaSpecification.LEFT_JOIN)
    .add( Restrictions.eq( 'name', 'Child1' ) )
    .list()

Why does this side-effect occur? I found a few discussions about using or avoiding this side-effect depending on your intended result, but nothing about why it is there in the first place and whether it was intended. The closest direct question is an old stale defect (http://opensource.atlassian.com/projects/hibernate/browse/HHH-3872).

  • EDIT 3/24: Fixed data *

解决方案

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

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

这篇关于在标准中使用左连接时的Hibernate Child Collection Limited的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆