Ste*_*ing 17 nhibernate lazy-loading icriteria
我有一个遗留数据库,我使用NHibernate进行映射.关注的对象是帐户和通知对象列表.对象看起来像:
public class Notification
{
public virtual int Id { get; set; }
public virtual DateTime BatchDate { get; set; }
/* other properties */
public virtual Account Account { get; set; }
}
public class Account
{
public virtual int Id { get; set; }
public virtual string AccountNumber { get; set; }
/* other properties */
}
Run Code Online (Sandbox Code Playgroud)
映射文件如下所示:
<class name="Account" table="Account" dynamic-update="true">
<id name="Id" column="AccountID">
<generator class="native" />
</id>
<property name="AccountNumber" length="15" not-null="true" />
<!-- other properties -->
</class>
<class name="Notification" table="Notification">
<id name="Id" column="Id">
<generator class="native" />
</id>
<!-- other properties -->
<many-to-one name="Account" class="Account" property-ref="AccountNumber" lazy="proxy">
<column name="AcctNum" />
</many-to-one>
Run Code Online (Sandbox Code Playgroud)
但是,当我创建一个标准,如
return session.CreateCriteria(typeof(Notification)).List<Notification>();
Run Code Online (Sandbox Code Playgroud)
我得到一个Select N + 1案例,即使从未引用过帐户,也会加载每个帐户.当多对一映射为惰性代理时,为什么所有帐户都被加载?
iam*_*ael 13
该问题是由property-ref属性引起的.延迟加载仅在many-to-one引用使用另一个对象的主键时起作用,因为NHibernate假定有一个外键约束强制执行此类值的有效性.使用非主键(由property-ref表示),NHibernate不做出这个假设,因此不假设相关对象必须存在.由于它不想为不存在的对象创建代理(即应该为null而不是代理),因此它急切地获取远程对象.not-found="ignore"指定时存在同样的问题,因为这表明未强制执行外键关系,并且可能导致空引用.
也可以看看:
| 归档时间: |
|
| 查看次数: |
8191 次 |
| 最近记录: |