I am using nhibernate subclass mapping to handle descriptions of objects of the system. Basically idea is to have a description class and make polymorphic associations by having sub classes specific to the object. My code inserts the description data correctly to the Database; that means the ownerid and ownertype columns are created correctly when I insert data.
But nhibernate is not using the OwnerType column for querying descriptions of Foo when I load Foo object using nhibernate.load() - It just queries the descriptions table using the ownerId column only which creates conflict since there can be other rows in the table with the same id but associated to a different ownertype.
Can you helop me find why it does not include ownerType for descriptions on the load query and how can I fix this? Thanks!
public class Product : EntityBase
{
public virtual string Name { get; set; }
public virtual IList<ProductDescription> Descriptions { get; set; }
}
public class ProductDescription : Description
{
public virtual Product Product { get; set; }
}
public class Description : EntityBase
{
public virtual string Name { get; set; }
public virtual string ShortDescription { get; set; }
public virtual string LongDescription { get; set; }
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test.Domain" assembly="Test.Domain">
<class name="Test.Domain.Product, Test.Domain" table="Products">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" />
<bag name="Descriptions" inverse="true" cascade="all">
<key column="OwnerID" />
<one-to-many class="Test.Domain.ProductDescription, Test.Domain" />
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Test.Domain" assembly="Test.Domain">
<class name="Test.Domain.Description, Test.Domain" table="Descriptions" discriminator-value="0">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<discriminator column="OwnerType" />
<property name="Name" />
<property name="ShortDescription" />
<property name="LongDescription" />
<subclass name="Test.Domain.ProductDescription, Test.Domain" discriminator-value="1">
<many-to-one name="Product" class="Test.Domain.Product, Test.Domain" column="OwnerID" not-null="true"></many-to-one>
</subclass>
</class>
</hibernate-mapping>