-->
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: Collections and DB supplied primary keys
PostPosted: Thu Feb 03, 2005 7:49 pm 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
I have done some research on this (google, 'in action', and forums) but haven't found a definative best practice.

I was looking for feedback on how to work with a collection of objects which do not yet have their key(id) defined. It is suggested that for one-many relationships the unordered type SET is the way to go. This works majestically in all instances but one I have found.

What happens if you want to add newly created objects to the collection before they have an ID assigned. Since the hashcode is generated by default from the ID field. All items overwrite each other and you end up having only the last one in the collection.

I know in theory we should be saving the items first to get the ID, and then adding them to the collection. But in some cases this is not really practical. Especially if you want to reuse the Hibernate Pojo's wrapped in a form bean for instance.

I saw a paragraph in 'in action' that mentions that 'bag' can be used, but it is not suggested. This is an issue because then the same element could be added to the collection twice. I guess you could modify the HashCode to use other fields, but I've read that is not recommended as well.

So in the end I would assume the only true way to work around this is to implement a bean identical to your Pojo with the exception of having the SET as ArrayList (or similar), as well as methods to convert from/to the POJO. I guess BeanUtil could be used to map across the values as well but I am not sure how this would handle the List --> Set mapping. PROBLEM: the big problem I have with this is that you have to then pass this extra object around through your business logic (along with the Hibernate Pojo hiearchy).

Sorry for the wordiness, but I am just not sure how best to do this. Looking for some enlightenment!

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 9:17 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Actually, I think if you do some searching on equals() and hashcode() you will see that hashing on id is not recommended. (especially in the case that you mentioned)


Top
 Profile  
 
 Post subject: solution (maybe)
PostPosted: Fri Feb 04, 2005 1:38 am 
Regular
Regular

Joined: Mon Nov 03, 2003 6:10 am
Posts: 75
Now that I gave it another look and after reading: http://www.hibernate.org/109.html

What about adding another attribute called transientId and modifying the hashcode to use it and the primary key (id) together. The transientId would have no mapping and would exist only in the POJO - specifically to act as a distiguisher for adding these unsaved keyless objects to the set.

I think that would work.. ??

Any one see anything wrong with that?


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.