子类的映射列表

本文关键字:列表 映射 子类 | 更新日期: 2023-09-27 18:03:01

我有一个关于列表的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>

注意子类"Condo"、"Parking"answers"Locker"(在我的例子中,只有Condo有额外的属性)。下面是使用这些子类列表的对象映射:

<?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>

每个列表都映射为一个包,指向相同的表,但根据列表(Condo, Parking和Locker)具有不同的类。

现在问题来了。问题是,当我试图访问任何这些列表,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 discriminating WHERE子句,以指定我想要的列表类型。我认为,如果我访问Parkings列表,我会看到一个WHERE [discriminator-column] = [discriminator-value],在我的情况下,WHERE parttype = 'park',但它不是在语句中,所以这就是为什么每一行得到返回。

我读到这可能是NHibernate中的一个bug(我目前使用3.1 GA版本),但阅读该bug的描述似乎是当列表的键在子类表中使用每个子类策略(join -subclass)时发生的,所以我不认为它适用于我的情况。

有人能帮我一下吗?我的映射文件有问题吗?为什么在SELECT中有双_id,为什么没有鉴别符WHERE子句?

子类的映射列表

简单的解决方法是在集合映射中添加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>