-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Superclass properties not saved by session.Save(subclass)
PostPosted: Wed Mar 19, 2008 10:20 am 
Newbie

Joined: Wed Mar 19, 2008 9:56 am
Posts: 2
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'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 2:21 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
The problem is (or may be) in your mapping. The <subclass> for C should be a child of the B-<subclass>, not its sibling.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 19, 2008 11:16 pm 
Newbie

Joined: Wed Mar 19, 2008 9:56 am
Posts: 2
You are right, thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.