栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

hibernte条件使用FetchType.EAGER多次返回子级

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

hibernte条件使用FetchType.EAGER多次返回子级

如果我正确理解您的配置,这实际上是预期的行为。

Order
在任何结果中都得到相同的实例,但是由于您现在正在使用进行联接
OrderTransaction
,因此它必须返回与常规sql联接将返回的结果相同的结果量

所以实际上它 应该 出现多次。这是由作者(是GavinKing)自己解释得非常好这里:它既解释了为什么,以及如何仍然得到不同的结果


在Hibernate FAQ中也提到过:

Hibernate不会为启用了集合的外部联接获取的查询返回不同的结果
(即使我使用了distinct关键字)?首先,您需要了解SQL以及OUTER
JOIN在SQL中的工作方式。如果您不完全理解和理解SQL中的外部联接,请不要继续阅读此FAQ项,而请查阅SQL手册或教程。否则,您将无法理解以下说明,并且会在Hibernate论坛上抱怨此行为。

可能返回相同Order对象的重复引用的典型示例:

List result = session.createCriteria(Order.class)         .setFetchMode("lineItems", FetchMode.JOIN)         .list();

<class name="Order">    ...    <set name="lineItems" fetch="join">

List result = session.createCriteria(Order.class) .list();List result = session.createQuery("select o from Order o left join fetch

o.lineItems”).list();

所有这些示例均产生相同的SQL语句:

>     SELECt o.*, l.* from ORDER o LEFT OUTER JOIN LINE_ITEMS l ON o.ID => l.ORDER_ID>  >> Want to know why the duplicates are there? Look at the SQL resultset,> Hibernate does not hide these duplicates on the left side of the outer> joined result but returns all the duplicates of the driving table. If you> have 5 orders in the database, and each order has 3 line items, the> resultset will be 15 rows. The Java result list of these queries will have> 15 elements, all of type Order. only 5 Order instances will be created by> Hibernate, but duplicates of the SQL resultset are preserved as duplicate> references to these 5 instances. If you do not understand this last> sentence, you need to read up on Java and the difference between an instance> on the Java heap and a reference to such an instance.

(为什么要使用左外部联接?如果您有其他没有订单项的订单,那么结果集将是16行,右侧填充NULL,而订单项数据用于其他订单。即使您想要订单,即使他们没有订单项,对吗?如果没有,请在您的HQL中使用内部联接提取)。

默认情况下,Hibernate不会过滤掉这些重复的引用。有些人(不是您)实际上想要这个。如何过滤掉它们?

像这样:

Collection result = new linkedHashSet( session.create*(...).list() );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401300.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号