我们最近升级到最新版本的 NHibernate (3.3.3.4001),我遇到了 NHibernate 2.1.2.4000 中不存在的问题。这让我相信这可能是新的内置字节码提供程序的问题。
考虑以下映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Foo.Core.Domain" assembly="Foo.Core" default-access="property">
<class name="EntityA" table="EntityA" lazy="true">
<id name="Id" column="EntityAId">
<generator class="native" />
</id>
<many-to-one name="EntityB" column="EntityBId" class="EntityB" not-null="true" />
<many-to-one name="EntityC" column="EntityCId" class="EntityC" not-null="true" access="readonly" insert="true" update="false" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Foo.Core.Domain" assembly="Foo.Core" default-access="property">
<class name="EntityB" table="EntityB" lazy="true">
<id name="Id" column="EntityBId">
<generator class="native" />
</id>
<many-to-one name="EntityC" column="EntityCId" class="EntityC" not-null="true" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Foo.Core.Domain" assembly="Foo.Core" default-access="property">
<class name="EntityC" table="EntityC" lazy="true">
<id name="Id" column="EntityCId">
<generator class="native" />
</id>
</class>
</hibernate-mapping>
这是我对 EntityA 的类定义:
Public Class EntityA
Public Overridable Property Id As Integer
Public Overridable Property EntityB As EntityB
Public Overridable ReadOnly Property EntityC As EntityC
Get
Return If(EntityB IsNot Nothing, EntityB.EntityC, Nothing)
End Get
End Property
End Class
当我为 EntityA 的实例调用 Session.Get 时存在问题 - 它会立即导致为其相应的 EntityB 发出选择:
Session.Get(Of EntityA)(id) ' Causes the EntityB that EntityA references to be loaded as well.
我最好的猜测是字节码提供程序导致在构建代理时评估我的只读“EntityC”属性,这会强制加载引用的EntityB。
有什么方法可以避免在 NHibernate 3.3.3 中使用这种类型的模型来避免急切的负载?