-->
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.  [ 1 post ] 
Author Message
 Post subject: Mapping two levels of inheritance with different strategies
PostPosted: Wed Jul 16, 2008 5:30 pm 
Newbie

Joined: Wed Jul 16, 2008 4:42 pm
Posts: 2
Hibernate version:
NHibernate 2.0 Beta 2

Hi,
this is probably a basic question and I am very sorry if it has been answered a thousand times.
For the past i-dont-know days I've been trying to understand how to map a "little" more complex class hierarchy then the quick start (and the Hibernate quick start) says.

The classes look like this:
Code:
internal abstract class BaseClass
{
    public virtual long ID { get; set; }
    public virtual string BaseText { get; set; }
}

internal class Sub1 : BaseClass
{
    public virtual string Sub1Text { get; set; }
}

internal abstract class Sub2 : BaseClass
{
    public virtual string Sub2Text { get; set; }
}

internal class Sub21 : Sub2
{
    public virtual string Sub21Text { get; set; }
}


Now this works perfectly well if I map everything with <joined-subclass> and the table per subclass strategy. However it might get pretty inefficient because the children of Sub2 are very similar or have no columns at all. So I want to map the first level by table per subclass and the second by table per class (sub?)hierarchy with Sub2 being "the" table.

I tried several mappings and with one all the Sub21 data went into the BaseClass table (which is perfectly understandable but doesn't make sense in this case) and I want it to go into Sub2 table.
This is the mapping that nearly does what I want (if my "want" is wrong -- correct me!):
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="ConsoleApplication20.BaseClass, ConsoleApplication20" abstract="true">
    <id name="ID">
      <generator class="native" />
    </id>

    <discriminator column="Disc" />
    <property name="BaseText" />

    <joined-subclass name="ConsoleApplication20.Sub1, ConsoleApplication20">
      <key column="ID" />
      <property name="Sub1Text" />
    </joined-subclass>

    <joined-subclass name="ConsoleApplication20.Sub2, ConsoleApplication20" abstract="true">
      <key column="ID" />
      <property name="Sub2Text" />
    </joined-subclass>

  </class>

  <subclass name="ConsoleApplication20.Sub21, ConsoleApplication20" extends="ConsoleApplication20.Sub2, ConsoleApplication20">
    <property name="Sub21Text" />
  </subclass>
</hibernate-mapping>

I put the last subclass out of Sub2 to get the <subclass>; I hope this is not some form of black voodoo magic.

This is what happens:
Code:
NHibernate: INSERT INTO BaseClass (BaseText) VALUES (@p0); select last_insert_rowid(); @p0 = 'ATXT'
NHibernate: INSERT INTO Sub2 (Sub2Text, Sub21Text, ID) VALUES (@p0, @p1, @p2); @p0 = 'bar2', @p1 = 'foo21Text', @p2 = '1'
NHibernate: INSERT INTO Sub2 (ID) VALUES (@p0); @p0 = '1'

As you can see, the last insert raises a constraint violation (same ID). Oh, I use SQLite by the way.

I hope you can help me with what I'd like to do (although I could just change the class model :)).

I asked a bunch of (Java-) people and they answered "well, we simply limited our class model to a depth of one". Which leaves my other question, should I make the model "simpler"?

Thanks a lot,
Jabe


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

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.