本文介绍了一对多关系在不使用“不同”的情况下获得重复的对象。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一对一关系中的2个类和一个有点奇怪的HQL查询。即使我已经阅读了一些已发布的问题,但对我而言似乎并不清楚。
班级部门{
@ OneToMany(fetch = FetchType.EAGER,mappedBy =department)
Set< Employee>雇员;
}
类员工{
@ManyToOne
@JoinColumn(name =id_department)
部门部门;
}
当我使用以下查询时,我得到重复的Department对象:
$ b session.createQuery(从Dep选择dep dep left join dep.employees);
因此,我必须使用distinct:
session.createQuery(加入dep.employees);
这种行为是否是预期的行为?我认为这是不寻常的,因为它与SQL进行了比较。 解决方案
这个问题在:
List结果= session.createCriteria(Order.class)
.setFetchMode(lineItems,FetchMode.JOIN)
.list();
< class name =Order>
< set name =lineItemsfetch =join>
...
< / class>
列表结果= session.createCriteria(Order.class)
.list();
列表result = session.createQuery(select o from Order o left join fetch o.lineItems)。list();
SELECT o。*,l。* from ORDER o LEFT OUTER JOIN LINE_ITEMS l ON o.ID = l .ORDER_ID
<$ p $
.setFetchMode(lineItems,FetchMode.JOIN)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
。列表结果= session.createCriteria(Order.class)名单();
< class name =Order>
...
< set name =lineItemsfetch =join>
列表结果= session.createCriteria(Order.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
List result = session.createQuery(select o from Order o left join fetch o.lineItems)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)//是的,真的!
.list();
列表结果= session.createQuery(从订单中选择不同的o,左连接读取o.lineItems)。
I have 2 classes in a one-to-many relationship and a HQL query that is a bit strange. Even if I have read some questions already posted, it does not seem clear to me.
Class Department{
@OneToMany(fetch=FetchType.EAGER, mappedBy="department")
Set<Employee> employees;
}
Class Employee{
@ManyToOne
@JoinColumn(name="id_department")
Department department;
}
When I use the following query I get duplicates Department objects:
session.createQuery("select dep from Department as dep left join dep.employees");
Thus, I have to use distinct:
session.createQuery("select distinct dep from Department as dep left join dep.employees");
Is this behaviour an expected one? I consider this unusual, as comparing it with SQL.
解决方案
This question is thoroughly explained on Hibernate FAQ:
List result = session.createCriteria(Order.class)
.setFetchMode("lineItems", FetchMode.JOIN)
.list();
<class name="Order">
<set name="lineItems" fetch="join">
...
</class>
List result = session.createCriteria(Order.class)
.list();
List result = session.createQuery("select o from Order o left join fetch o.lineItems").list();
SELECT o.*, l.* from ORDER o LEFT OUTER JOIN LINE_ITEMS l ON o.ID = l.ORDER_ID
List result = session.createCriteria(Order.class)
.setFetchMode("lineItems", FetchMode.JOIN)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
<class name="Order">
...
<set name="lineItems" fetch="join">
List result = session.createCriteria(Order.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.list();
List result = session.createQuery("select o from Order o left join fetch o.lineItems")
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) // Yes, really!
.list();
List result = session.createQuery("select distinct o from Order o left join fetch o.lineItems").list();
这篇关于一对多关系在不使用“不同”的情况下获得重复的对象。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!