-->
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.  [ 5 posts ] 
Author Message
 Post subject: Lists in Hibernate
PostPosted: Thu Sep 16, 2004 9:51 pm 
Newbie

Joined: Thu Sep 16, 2004 6:32 pm
Posts: 4
Hibernate version: 2

Hello, I have a question on how hibernate handles Lists. Our application has an object, say A, with an ordered collection of items (say a list of B objects) on it. We are supposed to persist the order in which the user adds B's to A, hence we are using a List. I fully understand that hibernate requires you to add an integer index column in the B table to keep track of the order of the List. My question is 'is there any way to have hibernate populate the index column automatically when it persists the List'? Currently we have to iterate over the List before persisting it and set the appropriate index on each item. This is fine, but it sucks having to put this DB-specific code in our objects and I would think this could be handled transparently by hibernate. Is there a better way to handle this?

Thx,
Andy


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 3:56 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Hibernate automaticly writes the index to the table when you persist the List collection. You will notice that index is not an attribute of your bean but it only exists in your table to assist Hibernate to find the index of the element in the List.
Your mapping should be like this:
Code:
<list name="myCollection" table="mycollection">
   <key column="myid"/>
   <index column="listindex"/>
...
</list>


Hope this helps.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 11:42 am 
Newbie

Joined: Thu Sep 16, 2004 6:32 pm
Posts: 4
Thanks for the reply, Dencel. I tried exactly what you said, but hibernate was leaving the index as 0 for every item. Consequently when I loaded the list from the DB, I would only get one of the items (presumably because they are all 0 and were being overwritten). The only way I was able to get it to work was to manually set the index prior to persisting. Any idea what I might be doing wrong? Here is a snippet from the xml file:

<list name="recordingMedia" cascade="all" inverse="true">
<key column="sessionId"/>
<index column="indx"/>
<one-to-many class="com.blah.RecordingMedia"/>
</list>


When I save a List of these recording media objects, the indx column is left at zero for every item. Any help would be greatly appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 12:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
http://www.hibernate.org/193.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 6:29 pm 
Newbie

Joined: Thu Sep 16, 2004 6:32 pm
Posts: 4
THANK YOU!!!


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