【发布时间】:2009-09-10 00:54:06
【问题描述】:
我要么误解了 NHibernate 手册,要么做错了什么。有人可以帮忙吗?
我正在尝试检索没有 AuditLogEntrys 的用户。 但 NHibernate 仍在加载 AuditLogEntrys。我只希望在访问属性时加载 AuditLogEntrys。
public class User
{
public virtual int UserId { get; set; }
public virtual string UserName { get; set; }
public virtual IList<AuditLogEntry> AuditLogEntrys { get; set; }
}
public class AuditLogEntry
{
public virtual int Id { get; set; }
public virtual DateTime DateRead { get; set; }
public virtual string MachineName { get; set; }
}
映射:
<class name="Model.User, Model"
table="User"
lazy="true">
<id name="UserId" access="property" column="UserID">
<generator class="native"></generator>
</id>
<property name="UserName" access="property" />
<bag name="AuditLogEntrys" lazy="true" access="property">
<key column="UserID" />
<one-to-many class="Model.AuditLogEntry, Model"></one-to-many>
</bag>
<class name="Model.AuditLogEntry, Model"
table="AuditLog"
lazy="true">
<id name="Id" access="property" column="ID">
<generator class="native"></generator>
</id>
<property name="DateRead" access="property" column="DateRead"></property>
<property name="MachineName" access="property" column="MachineName"></property>
</class>
获取用户的代码:
public IList<User> GetUserByUserName(string userName)
{
ICriteria criteria = NHibernateSession.CreateCriteria(typeof(User))
.Add(Expression.Eq("UserName", userName));
return GetByCriteria(criteria);
}
现在我希望 User 对象具有空的 AuditLogEntry 集合,但事实并非如此。
有什么想法吗?? 谢谢。
【问题讨论】:
-
您如何检查您的 AuditLogEntrys 集合?您是在调试器中检查它还是通过 SQL Profiler 观察?
-
我可以看到该集合已填充到调试器中