-->
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.  [ 2 posts ] 
Author Message
 Post subject: List Mapping Issue
PostPosted: Thu Feb 23, 2006 5:09 pm 
Newbie

Joined: Tue Apr 19, 2005 3:16 pm
Posts: 18
Hi, I'm using Hibernate version 3.1 on an Oracle 8i database. I have the following two (simplified) mapping documents:

Question.hbm.xml
Code:
<hibernate-mapping>
   <class name="Question">
      <id name="id" column="question_id"/>

      <list name="options" cascade="all">
         <key column="question_id"/>
         <list-index column="display_order"/>
         <one-to-many class="QuestionOption"/>
      </list>
   </class>
</hibernate-mapping>


QuestionOption.hbm.xml
Code:
<hibernate-mapping>
   <class name="QuestionOption">
      <id name="id" column="question_option_id"/>

      <many-to-one name="parentQuestion" class="Question" column="question_id"/>
   </class>
</hibernate-mapping>


The database tables are set up similar to this:
Code:
Questions:
   question_id (pk)

Question_Options:
   question_option_id (pk)
   question_id  (fk, not null)
   display_order


QuestionOption objects can't exist without a parent Question object, hence the not-null on the foriegn key. Everything works fine when I first insert a Question, but if I load a Question from the database and reorder the QuestionOption list, storing the Question object generates an error stating that NULL can't be inserted into QuestionOptions.question_id. Removing the not-null constraint on the foreign key fixes things, but results in the following SQL:

Code:
update Question_Options set question_id=null, display_order=null where question_id=? and question_option_id=?

update Question_Options set question_id=?, display_order=? where question_option_id=?


Why is this resulting in two SQL statements? I don't understand what I've done that makes Hibernate think the question_id and display_order columns need to be nulled before the display_order can be set to the proper value, or why it's removing and resetting the question_id value when all I'm doing is this (from the unit test for saving a reordered list):

Code:
// Code here grabs the session
Question question = (Question)session.createQuery("from Questions as q where q.id = 1").uniqueResult();
List options = question.getOptions();
Object o1 = options.remove(0);
options.add(o1);
session.save(question);
// Code here closes the session


Any pointer in the right direction would be appreciated there, as I'd rather not kill the referential integrity of the database in order to accomodate something that's most likely a mapping error on my part.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 5:58 pm 
Newbie

Joined: Tue Apr 19, 2005 3:16 pm
Posts: 18
Got it. Hidden in the reference document (in other words, right at the point where I stopped reading :P), is the answer. My mapping on the collection side needed to be:

<key column="question_id" not-null="true"/>

and on the member side:

<many-to-one name="parentQuestion" class="Question" column="question_id" insert="false" update="false"/>

Now it's just going to bug me wondering why this is. I can't think of a case where the default behavior would be desired. o.O


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.