I have an NHibernate problem with lists, which are mapped as subclasses of an abstract class.
First here is the mapping for the abstract class:
<?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>
Notice the subclasses "Condo", "Parking" and "Locker" (in my case, only Condo has additional properties). And this is the mapping of the object using lists of these sub开发者_运维问答classes:
<?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>
Each of the lists is mapped as a bag pointing to the same table but of different class depending on the list (Condo, Parking and Locker).
Now to the problem. The problem is that when I try to access any of those lists, NHibernate fetches all of the rows in the biz_syndicatepart
table and casting it to the proper class depending on the list. So let's say I have 3 rows in the table, if I access the Parkings
list, I'll have 3 parkings. If I access the Lockers
list, I'll have 3 lockers! Here is the SQL generated for the parkings list:
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 */
First thing, notice the double parkings0_.id
. Is this normal? Same thing for the parkings0_.syndicateid
(first and last row of the select statement). This I really don't understand.
Also, Notice that no discriminating WHERE
clause is inserted to specify which type of list I want. I would assume that if I would access the Parkings
list I would see a WHERE [discriminator-column] = [discriminator-value]
, in my case WHERE parttype = 'park'
but it's not in the statement so this is why every row gets returned.
I read that this might be a bug in NHibernate (I'm currently using version 3.1 GA) but reading the description of the bug it seems that it occurs when the key of the list is in the subclass table when using the table per subclass strategy (joined-subclass) so I don't think it applies to my situation.
Can someone help me with this?! Do I have a problem with my mapping files? Why the double _id
in the SELECT and why no discriminator WHERE
clause?
The simple workaround is to add a where clause to your collection mapping. E.g.
<bag name="Parkings" table="biz_syndicatepart" inverse="true" cascade="all-delete-orphan" where="parttype='park'">
<key column="syndicateid" />
<one-to-many class="Parking" />
</bag>
精彩评论