-->
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: Possible to a a List Element without saving the list parent?
PostPosted: Wed Aug 03, 2005 12:01 am 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
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():


Top
 Profile  
 
 Post subject: manual update
PostPosted: Wed Aug 03, 2005 3:15 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I think you need to update sequence number somehow manually, perhaps like this

int seq = 0;
List l = session.find( "select max(q.sequence) from Question q where q.block.id = ?", q.getId(), Type.INTEGER );
if( l != null && (!l.isEmpty)){
seq =((Integer) ((Object[]) .get(0))[0]).intValue();
)
q.setSequence( seq);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 03, 2005 11:54 am 
Beginner
Beginner

Joined: Wed Apr 13, 2005 2:03 pm
Posts: 34
Yah, a variant of that approach is already in place in my beforeInsert() handler. I was just hoping there was a way to get hibernate to do the lifting for me because I'm not really guaranteeing a contiguous list here. All I'm guaranteeing is that the new entity will (probably) have the next available sequence number, but my list could still have holes in it and/or be vulnerable to a race condition if two people added questions in *very* rapid succession :(.

Oh well, it's probably a risk I should just live with.


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.