我有一个像这样的接口:
public interface ICoreType {
int TypeID {get;}
}
和一个像这样实现它的 NHibernate 类:
public class DueDateType : ICoreType {
public virtual int DueDateTypeID {get;set;}
...
public virtual int TypeID { get{ return this.DueDateTypeID; } }
}
这是我的映射:
<class name="DueDateType" table="tdfDueDateType" lazy="true" >
<cache usage="read-write" include="all"/>
<id name="DueDateTypeID" column="DueDateTypeID" >
<generator class="identity"/>
</id>
...
</class>
当我从 NHibernate 获取对象的实例,然后将其返回到创建它的活动会话的范围之外时,我可以获取 DueDateTypeID 值就可以了,但是访问 TypeID 会抛出 LazyInitializationException。 TypeID 没有映射,因为我只是希望它返回 DueDateTypeID 的值。
在我的映射中标记类“lazy=”false”可以消除该问题。根据我的阅读,这是因为没有为非惰性映射类生成代理。
在这种情况下,我想使用 NHibernate 的延迟加载功能;我是否需要实现 IInterceptor 或类似的东西,以便让 NHibernate 以不同的方式处理这个属性?
I have an interface like this:
public interface ICoreType {
int TypeID {get;}
}
And an NHibernate class that implements it like this:
public class DueDateType : ICoreType {
public virtual int DueDateTypeID {get;set;}
...
public virtual int TypeID { get{ return this.DueDateTypeID; } }
}
Here's my mapping:
<class name="DueDateType" table="tdfDueDateType" lazy="true" >
<cache usage="read-write" include="all"/>
<id name="DueDateTypeID" column="DueDateTypeID" >
<generator class="identity"/>
</id>
...
</class>
When I get an instance of the object from NHibernate, and then return it outside the scope of the active session it was created in, I can get the value DueDateTypeID just fine, but accessing TypeID throws a LazyInitializationException. There is no mapping for TypeID, as I simply want it to return whatever DueDateTypeID's value is.
Marking the class lazy="false" in my mapping erases the problem. From what I've been reading, that is because there is no proxy generated for a non-lazy mapped class.
I would like to use the lazy loading features of NHibernate in this case; do I need to implement IInterceptor or some such, in order to get NHibernate to treat this property differently?
发布评论
评论(1)
问题是实体的所有成员在访问时都会触发延迟初始化(id 除外)。 NH 无法知道您在会员中实际在做什么。当在会话之外触发延迟初始化时,您会收到错误。
建议:
TypeID
,实体会被初始化,尽管这不是必需的,这对性能不利。The problem is that all members of the entity trigger lazy initialization when accessed (except of the id). NH can't know what you actually are doing within the member. When triggering lazy initialization outside of the session, you get an error.
Suggestions:
TypeID
, the entity is initialized although it is not necessary, which is bad for performance.