【发布时间】:2014-01-24 04:01:57
【问题描述】:
全部,
我有一个实体,它有几个集合,每个集合都是惰性映射的。当我运行条件查询时,我在结果集中得到了我的根实体的重复结果。当我所有的收藏都被懒惰地映射时,这怎么可能!
我验证了,我的收藏,懒加载。
这是我的地图:
根实体“项目”:
[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>();
我知道,我可以用 Distinct result transformer 纠正这个问题,我只是想知道这是否是惰性集合的正常行为。
编辑:我发现了这个问题的原因——查看原始 SQL 时,连接和 where 子句是正确的,但让我感到困惑的是生成的 Select 子句——它不仅包含来自项目实体的列(根实体),还有来自项目计划实体的列,导致我上面描述的问题。我现在不在工作,但我会尝试这样做:.SetProjection(Projections.RootEntity()),所以我只在 select 子句中获取 Project 的列。
【问题讨论】:
-
我想我找到了我的问题的副本:floledermann.blogspot.ca/2007/10/…
-
跟进上面的博客,貌似.setFirstResult(start).setMaxResults(pageSize) 是EVIL!
标签: asp.net nhibernate nhibernate-criteria