Nhibernate:第二级集合中的不同结果 [英] Nhibernate: distinct results in second level Collection

查看:69
本文介绍了Nhibernate:第二级集合中的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的对象模型:

I have an object model like this:

   class EntityA
   {
        ...
        IList<EntityB> BList;
        ...
   }

   class EntityB
   {
       ...
       IList<EntityC> CList;
   }

我必须获取所有集合(在EntityA中的Blist和在EntityB中的CList),因为如果需要它们全部进行一些操作,如果我不急于加载它们,那么我将遇到选择n + 1问题. 所以查询是这样的:

I have to fetch all the colelctions (Blist in EntityA and CList in EntityB), because if they all will be needed to make some operations, if i don't eager load them i will have the select n+1 problem. So the query was this:

  select a from EntityA a left join fetch a.BList b left join fetch b.CList c

此查询遇到的第一个问题是从数据库返回重复项,因为与BList进行了左联接获取,所以我有EntityA重复项. 快速浏览了hibernate文档,并找到了一些解决方案,首先我尝试了distinct关键字,该关键字可能不会复制SQL distinct关键字,但在某些情况下,也许是其中一种情况,因为我遇到了一个SQL错误,说我无法选择离散文本列(EntityA表中的[Observations]列).因此,我使用了其他解决方案之一:

The fist problem i faced with this query, was the return of duplicates from the DB, i had EntityA duplicates, because of the left join fetch with BList. A quick read through the hibernate documentation and there were some solutions, first i tried the distinct keyword that supposelly wouldn't replicate the SQL distinct keyword except in some cases, maybe this was one of those cases because i had a SQL error saying that i cannot select distict text columns (column [Observations] in EntityA table). So i used one of the other solutions:

  query.SetResultTransformer(new DistinctRootEntityResultTransformer());

这很好.但是操作结果仍然没有通过测试.我进一步检查,发现由于EntityList的左联接获取,现在存在EntityB的重复项.

This worked fine. But the result of the operations were still not passing the tests. I checked further and i found out that now there were duplicates of EntityB, because of the left join fetch with CList.

问题是,如何在第二级收藏中使用非重复?我进行搜索,但只找到根实体的直接子集合的解决方案,而没有找到第二级子集合的解决方案...

The question is, how can i use the distinct in a second level collection? I searched and i only find solutions for the root entity's direct child collection, but never for the second level child collections...

谢谢您的时间

推荐答案

我遇到了相同的问题,但无法通过hql解决重复的问题.但是,我为所有集合创建了IEqualityComparer,并在每个集合上执行了Disinct()以消除hql结果之上的重复项.

I encountered the same problem and was not able to solve the duplicate issue through hql. However, I created IEqualityComparer for all collections and did Disinct() on each Collection to eliminate duplicates on top of the hql result.

这篇关于Nhibernate:第二级集合中的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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