NHibernate的:不能成功地设置延迟加载 [英] NHibernate: can't successfully set lazy loading

查看:127
本文介绍了NHibernate的:不能成功地设置延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父表和一张桌子儿童。儿童包含一个外键的父表,创建一个一对多的关系。这是我的映射我用流利的NHibernate定义的一部分:

I have a table Parent and a table Child. Child contains a foreign-key to the Parent table, creating a one-to-many relationship. Here is a part of my mapping that I define with fluent NHibernate:

public class ParentMap : ClassMap<Parent>
{
    public ParentMap()
    {
        WithTable("Parents");

        Id(x => x.Id, "ParentID")
    	.WithUnsavedValue(0)
    	.GeneratedBy.Identity();

        Map(x => x.Description, "Description");

        HasMany<Child>(x => x.Childs)
    	.LazyLoad()
    	.WithKeyColumn("ParentID")
    	.IsInverse()
    	.AsSet();
    }
}

public class ChildMap : ClassMap<Child>
{
    public ChildMap()
    {
        WithTable("Childs");

        Id(x => x.Id, "ChildID")
    	.WithUnsavedValue(0)
    	.GeneratedBy.Identity();

        References(x => x.Parent, "ParentID")
        	.CanNotBeNull()
        	.LazyLoad();
    }
}

正如你可以看到我对关系设置LazyLoad。还要注意的是在我的模型类,所有的属性都设置为虚拟的。

As you can see I have set LazyLoad on the relation. Note also that in my model classes, all properties are set as virtual.

现在的简单查询:

ICriteria crit = Session.CreateCriteria(typeof(Child))
    .Add(Expression.Eq("Id", 18));
IList<Child> list = crit.List<Child>();

和生成的SQL:

SELECT this_.ChildID            as ChildID5_1_,
       this_.ParentID           as ParentID5_1_,
       parent2_.ParentID    as ParentID4_0_,
       parent2_.Description as Descript2_4_0_
FROM   Childs this_
       inner join Parents parent2_
         on this_.ParentID = parent2_.ParentID
WHERE  this_.ChildID = 18 /* @p0 */

正如你所看到的,它做了一个连接的父表并选择它的字段(ID和说明)。但是,为什么会这样,因为我要求lazyloading?

As you can see, it does a join on the Parent table and selects its fields (id and description). But why does it do that since I requested lazyloading ?

现在如果我改变了查询:

Now if I change the query to:

ICriteria crit2 = Session.CreateCriteria(typeof(Child))
    .SetFetchMode("Parent", FetchMode.Lazy)
    .Add(Expression.Eq("Id", 18));

有产生2 SQL查询:

There are 2 sql queries generated:

SELECT this_.ChildID  as ChildID5_0_,
       this_.ParentID as ParentID5_0_
FROM   Childs this_
WHERE  this_.ChildID = 18 /* @p0 */

这是很好的对我说:不加入,父表不查询。 但我得到这个第二个太:

which is good to me: no join, the Parent table is not queried. But I get this second one too:

SELECT parent0_.ParentID    as ParentID4_0_,
       parent0_.Description as Descript2_4_0_
FROM   Parents parent0_
WHERE  parent0_.ParentID = 45 /* @p0 */

这再次查询父表。

which again queries the Parent table.

在该行过程中产生的这2个查询:

These 2 queries are generated during the line:

IList<Child> list = crit.List<Child>();

我完全不了解这里发生的事情。有人可以帮忙吗?

I'm totally ignorant of what happens here. Can someone help?

推荐答案

这将取决于你的流利的NHibernate的版本。直到某一点上,它是默认的,所有的实体将的没有的是延迟加载。这是显式设置懒惰=假在实体相当。这已不再是个案,但如果你是在什么之前该点运行,那么你会看到这种行为。

It'll depend on your version of Fluent NHibernate. Up until a certain point it was the default that all entities would not be lazy loaded. This is the equivalent of explicitly setting lazy="false" in your entity. This is no longer the case, but if you are running on anything prior to that point then you'll see this behaviour.

在许多到一/引用延迟加载设置,得到由来自目标的实体层面延迟加载覆盖,因此,如果您是在FNH这个老版本运行,那么该实体设置将渲染你的引用(...)。LazyLoad()的呼叫没有实际意义。

The many-to-one/references lazy load setting gets overridden by the entity level lazy load from the target, so if you are running on this older version of FNH then the entity setting will be rendering your References(...).LazyLoad() call moot.

您需要验证你在最新版本的FNH的,这应该解决的事情;但是,如果没有的话,你需要明确设置延迟加载在你的实体。你可以做到这一点与上 ClassMap&LT的 LazyLoad 方法; T&GT;

You need to verify you're on the latest version of FNH, that should fix things; however, if it doesn't then you need to explicitly set lazy loading on in your Parent entity. You can do that with the LazyLoad method on the ClassMap<T>.

这篇关于NHibernate的:不能成功地设置延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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