I've got a situation where I have a "block" class which contains within it a list of questions (mapping below).
For reasons mostly having to do with the web framework I'm using, I will, from time to time, need to add questions without being able to update the block I currently have in memory.
So what I've been doing is:
Block b = Session.get(someblock)
Question q = new Question();
q.setBlock(b);
... lots of user i/o screens, strangeness
Session.save(q);
The problem I'm running into is that Hibernate correctly associates q with block b, but it doesn't properly set it sequence number in the list. Thus if Block b had questions 0,1,2,3 before I added q, it'll have 0,1,2,3, *0* after I've added q because q goes in with the default (0) sequence number.
Is there a way for me to tell hibernate to calculate the right sequence number if I add a list element like this without updating the "parent" who owns the list?
Mapping below (with some extraneous stuff clipped out).
Hibernate version:
3.0.3
Mapping documents:
<class name="survey.Question" table="surveyquestion">
<cache usage="read-write"></cache>
<id name="id" type="string" length="32">
<column name="id" length="32" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="text" column="text" type="string"/>
<property name="type" column="type" type="int" />
<property name="sequence" column="sequence" type="int" />
<many-to-one name="block" class="survey.Block" >
<column name="block_id" length="32"></column>
</many-to-one>
</class>
<class name="survey.Block" table="surveyblock">
<cache usage="read-write"></cache>
<id name="id" type="string" length="32">
<column name="id" length="32" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="blockName" column="blockname" />
<property name="blockCode" column="blockcode" type="string" length="3"/>
<list name="questions">
<key column="block_id"></key>
<list-index column="sequence"/>
<one-to-many class="survey.Question"/>
</list>
</class>
Code between sessionFactory.openSession() and session.close():
|