-->
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: Inheritance mapping for a subclass that has an additional PK
PostPosted: Mon Mar 16, 2009 6:49 pm 
Newbie

Joined: Mon Mar 16, 2009 6:33 pm
Posts: 2
I have this abstract Answer class that has subclasses like AnswerDate,AnswerNumber and AnswerOption depending on the type of the answers.

The Answer class has an id (QuestionnaireId, QuestionId)
All tables except for AnswerOption can be uniquely identified by this id.

AnswerOption needs a primary key of (QuestionnaireId, QuestionId, OptionId) as there could multiple options selected as an Answer. I am having difficulty creating the hibernate mapping for this.
the following mapping that we have doesn't work.


<hibernate-mapping>
<class name="model.Answer" abstract="true">
<composite-id name="id" class="model.QuestionAnswerId">
<key-property name="questionId" type="long">
<column name="QUESTION_ID" precision="6" scale="0" />
</key-property>
<key-property name="questionnaireId" type="long">
<column name="QUESTIONNAIRE_ID" precision="6" scale="0" />
</key-property>
</composite-id>

<property name="lastModifiedByUserId" type="string">
<column name="LAST_MODIFIED_BY_USER_ID" length="10" />
</property>
<property name="lastModifiedTimestamp" type="timestamp">
<column name="LAST_MODIFIED_TIMESTAMP" length="23" />
</property>
..
..
..
<union-subclass name="model.AnswerText" table="QS_ANSWER_TEXT">
<property name="value" type="java.lang.String" >
<column name="ANSWER_TEXT"/>
</property>
</union-subclass>
<union-subclass name="model.AnswerOption" table="QS_ANSWER_OPTION">
<many-to-one name="option" class="model.Option" fetch="select" insert="false" update="false">
<column name="OPTION_ID" />
</many-to-one>
</union-subclass>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 7:50 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
I doubt if you can have a inheritance mapping if the super class and sub class are having the same identifier. So I think you will have to have a restructure of your associations. The relation should be 1 question can have only one answer ( and vice versa) but in case of AnswerOption, 1 answer can have many right options and 1 option can be related to answers of different questions (ie many answers).

ie. All the Answer tables will have same primary key (QuestionnaireId, QuestionId). But an AnswerOption have a many-to-many relation to Option.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 3:35 pm 
Newbie

Joined: Mon Mar 16, 2009 6:33 pm
Posts: 2
Thanks Litty!! that is indeed the right way to go about this, considering the design aspect.. I did a quick prototype and it worked well...


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.