-->
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: Multiple tables for instances of the same class?
PostPosted: Sat Nov 19, 2005 2:25 am 
Newbie

Joined: Wed Nov 16, 2005 2:40 pm
Posts: 19
Location: Palo Alto, CA
Hi

I have encountered a strange problem with NHibernate. I have a class named Question, that has two subclasses. All three classes can be instantiated. I model them with joined-subclass elements as shown below:

Code:
  <class name="CVAS.Primitives.Question, Test" table="Questions">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Prompt" not-null="true" />
  </class>
  <joined-subclass name="CVAS.Primitives.CategoricalQuestion, Test" extends="CVAS.Primitives.Question, Test" table="CategoricalQuestions">
    <key column="QuestionID" />
    <list name="OptionsList" table="CategoricalOptions">
      <key column="QuestionID" />
      <index column="onum" />
      <element column="Value" type="string" not-null="true" />
    </list>
  </joined-subclass>
  <joined-subclass name="CVAS.Primitives.CompoundQuestion, Test" extends="CVAS.Primitives.Question, Test" table="CompoundQuestions">
    <key column="QuestionID" />
    <list name="QuestionList" table="ContainedQuestions" cascade="all-delete-orphan">
      <key column="ID" />
      <index column="qnum" />
      <one-to-many class="CVAS.Primitives.Question, Test" />
    </list>
  </joined-subclass>


When I build my solution, I get the following items (among others) in the log:


Code:
2005-11-18 22:17:45,391 [4796] DEBUG NHibernate.Cfg.Binder [(null)] <(null)> - Second pass for collection: CVAS.Primitives.CategoricalQuestion.OptionsList
2005-11-18 22:17:45,391 [4796] DEBUG NHibernate.Cfg.Binder [(null)] <(null)> - Mapped collection key: QuestionID, index: onum, element: Value, type: String
2005-11-18 22:17:45,391 [4796] DEBUG NHibernate.Cfg.Binder [(null)] <(null)> - Second pass for collection: CVAS.Primitives.CompoundQuestion.QuestionList
2005-11-18 22:17:45,391 [4796] INFO  NHibernate.Cfg.Binder [(null)] <(null)> - mapping collection: CVAS.Primitives.CompoundQuestion.QuestionList -> Questions
2005-11-18 22:17:45,391 [4796] DEBUG NHibernate.Cfg.Binder [(null)] <(null)> - Mapped collection key: ID, index: qnum, one-to-many: Question


I noticed that qnum, the index of the list, gets mapped to the table Question, rather than to CompoundQuestion as declared. This made me wonder how I model the relationship in which CompoundQuestion, a subclass of Question can hold a list of instances of Question. I wasn't able to find any examples that clearly demonstrated what I would like to do. While I could conceivably refactor my CompoundQuestion to be a non-Question, I was wondering if there was some way to model the structure as I have it now.

Thanks for any insight you have on this problem!

Gene


Top
 Profile  
 
 Post subject: Re: Multiple tables for instances of the same class?
PostPosted: Tue Mar 14, 2006 4:05 am 
Newbie

Joined: Tue Mar 14, 2006 2:22 am
Posts: 3
Hi,

I have the same problem and I'm looking for a useful solution too. At the
moment I'm using JAVA inheritance and @MappedSuperclass annotation to
build subclasses for which I can set target table.
I don't like this approch because I need to create a lot of subclasses only
because I need to change the target table.

What is your current approch?

Regards,
Christoph


Top
 Profile  
 
 Post subject: Re: Multiple tables for instances of the same class?
PostPosted: Tue Mar 14, 2006 1:47 pm 
Newbie

Joined: Wed Nov 16, 2005 2:40 pm
Posts: 19
Location: Palo Alto, CA
Christoph wrote:
Hi,

I have the same problem and I'm looking for a useful solution too. At the
moment I'm using JAVA inheritance and @MappedSuperclass annotation to
build subclasses for which I can set target table.
I don't like this approch because I need to create a lot of subclasses only
because I need to change the target table.

What is your current approch?

Regards,
Christoph


My question was based on a misconception of how Hibernate maps objects onto tables. The way it works is that there is a single table for each class (subclasses can either have their own tables, or have their properties stored in the same table as the base class). What I had failed to understand was how one-t0-many relationships are represented. If you have two classes, A and B, each of which contains a collection (say an IList) of class C, you still store all instances of C in a single table, but for each one-to-many relationship in each class (A,B), you add a column to the table that stores C. This column (added where the IList property is defined in A or B) represents the position of the given instance of C in the IList in question. When you persist the IList, Hibernate automatically updates that column to reflect the order of objects in the IList. If you're using sets, then no additional column is needed.

Finally, and this is cruicial, after you add objects (instances of C) to the IList, you must call the Flush() method to get the data persisted correctly. Without it, your collections may wind up empty.

Hope this helps,

Gene


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.