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 */
}
マッピングファイルは次のようになります。
<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>
ただし、次のような基準を作成すると
return session.CreateCriteria(typeof(Notification)).List<Notification>();
アカウントが参照されていなくても、各アカウントが読み込まれるSelect N+1のケースが発生します。多対1がレイジープロキシとしてマッピングされているのに、なぜすべてのアカウントが読み込まれるのですか?