映射子类列表

时间:2011-07-19 14:35:13

标签: c# .net nhibernate subclass

我有一个列表的NHibernate问题,它被映射为抽象类的子类。

首先是抽象类的映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="false"
                   assembly="Magma.Core"
                   namespace="Magma.Core.Business">
    <class name="SyndicatePart" table="biz_syndicatepart" abstract="true" lazy="false">
        <id name="Id" column="id">
            <generator class="guid.comb" />
        </id>
        <discriminator column="parttype" not-null="true" />

        <property name="Identifier" column="name" not-null="true" />
        <property name="Share" column="share" not-null="true" />
        <property name="CadasterNumber" column="cadaster_number" not-null="true" />

        <many-to-one name="Account" column="accountid" lazy="proxy" cascade="all" />
        <many-to-one name="Syndicate" column="syndicateid" lazy="proxy" cascade="all" />

        <subclass name="Condo" discriminator-value="condo" lazy="false">
            <property name="OwnerType" column="ownertype" />

                <many-to-one name="Building" column="buildingid" />
            <many-to-one name="Address" column="addressid" />

            <bag name="Tenants" access="field.camelcase-underscore" table="biz_tenant" inverse="true" cascade="all-delete-orphan">
                <key column="syndicatepartid" />
                <one-to-many class="Tenant" />
            </bag>
        </subclass>

        <subclass name="Parking" discriminator-value="park" lazy="false" />
        <subclass name="Locker" discriminator-value="lock" lazy="false" />
    </class>
</hibernate-mapping>

注意子类“公寓”,“停车”和“储物柜”(在我的情况下,只有公寓有其他属性)。这是使用这些子类的列表对象的映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Magma.Core"
                   namespace="Magma.Core.Business">
    <class name="Syndicate" table="biz_syndicate" abstract="true" lazy="false">
        <id name="Id" column="id">
            <generator class="guid.comb" />
        </id>
        <discriminator column="orientation" not-null="true" />

        <property name="Name" column="name" not-null="true" />

        <many-to-one name="Manager" column="managerid" cascade="all-delete-orphan" />

        <bag name="Buildings" table="biz_building" inverse="true" cascade="all-delete-orphan">
            <key column="syndicateid" />
            <one-to-many class="Building" />
        </bag>

        <bag name="Parkings" table="biz_syndicatepart" inverse="true" cascade="all-delete-orphan">
            <key column="syndicateid" />
            <one-to-many class="Parking" />
        </bag>

        <bag name="Lockers" table="biz_syndicatepart" inverse="true" cascade="all-delete-orphan">
            <key column="syndicateid" />
            <one-to-many class="Locker" />
            </bag>

        <subclass name="VerticalSyndicate" discriminator-value="vertical" lazy="false" />
        <subclass name="HorizontalSyndicate" discriminator-value="horizontal" lazy="false" />
    </class>
</hibernate-mapping>

每个列表都被映射为一个指向同一个表但不同类别的包,具体取决于列表(公寓,停车场和储物柜)。

现在问题。问题是,当我尝试访问任何这些列表时,NHibernate会获取biz_syndicatepart表中所有行的所有,并根据列表将其强制转换为正确的类。所以假设我在表格中有3行,如果我访问Parkings列表,我将有3个停车位。如果我访问Lockers列表,我将有3个储物柜!这是为停车列表生成的SQL:

SELECT parkings0_.syndicateid     as syndicat7_1_,
       parkings0_.id              as id1_,
       parkings0_.id              as id39_0_,
       parkings0_.name            as name39_0_,
       parkings0_.share           as share39_0_,
       parkings0_.cadaster_number as cadaster5_39_0_,
       parkings0_.accountid       as accountid39_0_,
       parkings0_.syndicateid     as syndicat7_39_0_
FROM   biz_syndicatepart parkings0_
WHERE  parkings0_.syndicateid = '2310fcdf-8ab3-48dd-9a75-9f1e00f6f4fd' /* @p0 */

首先,请注意双parkings0_.id。这是正常的吗? parkings0_.syndicateid(select语句的第一行和最后一行)也是如此。这个我真的不明白。

另请注意,插入 no 判别WHERE子句以指定我想要的列表类型。我假设如果我访问Parkings列表,我会看到WHERE [discriminator-column] = [discriminator-value],在我的情况下WHERE parttype = 'park',但它不在语句中,所以这就是每行返回的原因。

我读到这可能是NHibernate中的一个错误(我目前正在使用版本3.1 GA)但是阅读错误的描述似乎当使用表时列表的键位于子类表中​​时会发生错误每个子类策略(加入子类)所以我认为它不适用于我的情况。

有人可以帮我这个吗?!我的映射文件有问题吗?为什么SELECT中的_id加倍,为什么没有鉴别器WHERE子句?

1 个答案:

答案 0 :(得分:2)

简单的解决方法是在集合映射中添加where子句。例如。

    <bag name="Parkings" table="biz_syndicatepart" inverse="true" cascade="all-delete-orphan" where="parttype='park'">
        <key column="syndicateid" />
        <one-to-many class="Parking" />
    </bag>