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.xmlCode:
<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.