NHibernate version: 2.0
Hi all,
This is a dead-simple distillation of the problem I've run into. There is a hierarchy with three classes:
class A - has no properties
class B - subclass of A, has int property 'num'
class C - subclass of B, has no properties
This simple hierarchy is mapped with table-per-hierarchy strategy. When session.Save() is invoked on an instance of class C, NHibernate fails to save its inherited property 'num' to the database - the corresponding column gets the NULL value.
The workaround is to move the property 'num' into class A, the base class of the hierarchy. Then, HNibernate properly saves the value of 'num' for instances of classes B or C. Needless to say, this workaround is a rather ugly encapsulation-breaking hack that I'd prefer to avoid.
Has anyone encoutered this situation before? Any ideas appreciated.
Here is the simple mapping file for the example above that reproduces the problem. If the property 'num' is moved from class B to base class A, then NHibernate saves it properly for instances of B or C.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ... [snip]>
<class name="A" table="dbo.A" discriminator-value="A">
<id type="Int32" column="AId" name="m_id" access="field">
<generator class="identity"/>
</id>
<discriminator type="String" length="8" column="ItemType"/>
<subclass name="B" discriminator-value="B">
<property name="num" access="field" column="num"/>
</subclass>
<subclass name="C" discriminator-value="C">
</subclass>
</class>
</hibernate-mapping>
Corresponding C# code:
public class A
{
public A() {}
private int m_id;
public virtual int Id { get { return m_id; } }
}
public class B: A
{
public B() {}
protected int num;
}
public class C: B
{
public C() {}
}
...
C obj = new C();
session.Save(C); // this does not properly save inherited property 'num'
|