NHibernate的 - 重复记录与懒洋洋地映射集合 [英] NHibernate - Duplicate Records with lazily mapped collection

查看:449
本文介绍了NHibernate的 - 重复记录与懒洋洋地映射集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我有一个实体,那有几个收藏, - 每个集合映射懒洋洋地。当我运行条件查询,我得到的结果集中我的根实体重复的结果。这怎么可能当我所有的收藏品都懒洋洋地映射!

I have an entity, that has several collections,- each collection is mapped lazily. When I run a criteria query, I get duplicate results for my root entity in the result set. How's that possible when all my collections are mapped lazily!

我核实,我的收藏​​,懒洋洋地加载

I verified, my collections, load lazily.

下面是我的映射:

根实体项目:

[Bag(0, Lazy = CollectionLazy.True, Inverse = true, Cascade = "all-delete-orphan")]
    [Key(1, Column = "job_id")]
    [OneToMany(2, ClassType = typeof(ProjectPlan))]
    public virtual IList<ProjectPlan> PlanList
    {
        get { return _planList; }
        set { _planList = value; }
    }

的标准查询是:

    ICriteria criteria = session.Session.CreateCriteria<Entities.Project>()
                    .Add(Restrictions.Eq(Entities.Project.PROP_STATUS, !Entities.Project.STATUS_DELETED_FLAG));
                    .CreateAlias(Entities.Project.PROP_PLANLIST, "p")
                    .Add(Restrictions.Eq("p.County", 'MIDDLSEX'))
.setFirstResult(start).setMaxResults(pageSize)
                    .List<Entities.Project>();

我知道,我可以纠正这个问题瓦特/鲜明变压器的结果,我只是想知道,如果这是懒惰的藏品正常的行为。

I know, I can correct this problem w/ Distinct result transformer, I just want to know if this is normal behavior on lazy collections.

编辑:我发现这个原因, - 看时的原始SQL,联接和WHERE子句是正确的,但什么令我感到困惑的是生成的SELECT子句 - 它不仅包含从项目实体列(根实体),但也列在项目计划的实体,导致我上述的问题。我不上班的权利,但我会尽力做到这一点:.SetProjection(Projections.RootEntity()),所以我只得到了SELECT子句中的项目的列

I found the cause of this,- when looking at the raw SQL, the join, and where clause are correct but what baffles me is the generated Select clause,- it not only contains columns from the project entity (root entity) but also columns from the project plans entity which causes the issue I described above. I am not at work right now, but I'll try to do this: .SetProjection(Projections.RootEntity()), so I only get Project's columns in the select clause.

推荐答案

的一种方式,该如何解决这个的(我会这么说通常情况下)的是:1)不使用取里面收藏查询和2)使用批量抓取,作为映射的一部分

One way, how to solve this (I'd say so usual scenario) is: 1) not use fetching collections inside of the query and 2) use batch fetching, as a part of the mapping

所以,我们总是会查询根实体。这将会给我们一个平坦的结果集,其中的可以正确使用的页面即可。

So, we will always be querying the root entity. That will give us a flat result set, which can be correctly used for paging.

要获得每个收到行收集数据,以避免1 + N的问题(goign为每条记录的集合),我们将使用的 19.1.5。使用批量抓取

To get the collection data for each recieved row, and to avoid 1 + N issue (goign for collection of each record) we will use 19.1.5. Using batch fetching

映射会是这样

[Bag(0, Lazy = CollectionLazy.True
      , Inverse = true
      , Cascade = "all-delete-orphan"
      , BatchSize = 25)] // Or something similar to batch-size="25"
[Key(1, Column = "job_id")]
[OneToMany(2, ClassType = typeof(ProjectPlan))]
public virtual IList<ProjectPlan> PlanList
{
   ...

一些其他类似的QA的(以几乎相同的细节)

  • How to Eager Load Associations without duplication in NHibernate?
  • NHibernate QueryOver with Fetch resulting multiple sql queries and db hits
  • Is this the right way to eager load child collections in NHibernate

和我们仍然可以过滤器收集项!但是,我们必须使用子查询,一个例子查询

And we still can filter over the collection items! but we have to use subqueries, an example Query on HasMany reference

这篇关于NHibernate的 - 重复记录与懒洋洋地映射集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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