【发布时间】:2013-09-12 12:03:25
【问题描述】:
我们最近升级到了最新版本的 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 中使用这种类型的模型来避免急切负载的发生?
【问题讨论】:
-
抱歉,如果是愚蠢的评论,但是根据 EntityA.EntityC 属性代码,我没有看到 EntityA.EntityC 在映射中的意义(不知道您可以在 VB 中定义显式只读属性.Net,而 AFAIK,你不能在 C# 中)
-
要清楚,在我的实际实现中,这背后有一点(这只是一个人为的例子来说明问题) - 我需要该列用于非规范化目的(报告)。
-
我知道您为了简单起见设置了这个清晰的示例。我只是对 EntityA.EntityC 的这两个具有不同语义的定义感到困惑,一个在映射中,一个在代码中,并且想知道代理类的预期行为。也许您可以对此有所了解,因为它似乎是您问题的核心。
标签: nhibernate