我当前的项目中有一个奇怪的问题。查询的延迟加载不起作用。当我查询一个列表时,nhibernate 会分别获取所有关联。
我提取了其中的一小部分并将其放入单独的解决方案中。基本上我现在得到的是一个 Account-Table 和一个 AccountSync-Table。两者都有一个 ID 和一个 URL,而 ID 只是一个 db-guid。
我的课程是:
public class HippoAccount
{
public virtual Guid Id { get; set; }
public virtual string Url { get; set; }
public virtual HippoAccountSync Sync { get; set; }
}
public class HippoAccountSync
{
public virtual Guid Id { get; set; }
public virtual string Url { get; set; }
public virtual HippoAccount Account { get; set; }
}
当我现在通过它的 guid 加载一个对象时:
var account = session.Load<HippoAccount>(accountId);
Console.WriteLine(NHibernateUtil.IsPropertyInitialized(account, "Sync"))
...它返回false
并且帐户本身就是一个代理。
但是通过条件 API 加载列表时:
var account = (HippoAccount)session
.CreateCriteria(typeof (HippoAccount))
.Add(Restrictions.Eq("Id", accountId))
.List()[0];
...属性Sync
被初始化(触发第二个选择查询),并且返回的对象不是代理。
这是默认行为吗?我怎么了?
映射是:
<class name="HippoAccount" table="AllAccounts">
<id name="Id" type="guid">
<generator class="guid"/>
</id>
<property name="Url" />
<many-to-one
class="HippoAccountSync"
name="Sync"
not-found="ignore"
property-ref="Url">
<column name="url" />
</many-to-one>
</class>
<class name="HippoAccountSync"
mutable="false"
table="Accounts">
<id name="Id" type="guid">
<generator class="guid"/>
</id>
<property name="Url">
<column name="serviceUri" />
</property>
<many-to-one class="HippoAccount"
name="Account"
property-ref="Url"
not-found="ignore">
<column name="serviceUri" />
</many-to-one>
</class>