-->
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: Questions about one-to-many and Set.add semantics
PostPosted: Fri Dec 12, 2003 5:33 pm 
Newbie

Joined: Mon Nov 10, 2003 4:27 pm
Posts: 13
I'm trying to map a one-to-many relationship and get an insert or update of the parent object to cause a Set of child objects to be inserted into the database with their foreign keys set automatically to the parent key, etc.

The issue is that the implementation of the Set.add method wants a unique object but the objects aren't unique until they get an ID from the database. So I'm forced to save each child object individually and then add them to the parent object. I'd really like hibernate to do all the work and insert them automagically when I update the parent object:)

Is it as simple as overriding the equals method of the child object so they appear to be unique even if their ID's are null? Or is hibernate depending on the equals method in some way so that I can't change it?

Thanks,
Rich


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 10:04 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Is it as simple as overriding the equals method of the child object so they appear to be unique even if their ID's are null?


Remember that you can't change the hashcode of an object while it is in a set. So no, no-one has ever had a good solution for this.

I generally advise not to make equals to an id comparison. It should compare "business keys", not surrogate keys.

(Alternatively, use a <bag>.)


Top
 Profile  
 
 Post subject: Could you clarify what you mean by...
PostPosted: Mon Dec 15, 2003 12:08 pm 
Newbie

Joined: Mon Nov 10, 2003 4:27 pm
Posts: 13
"you can't change the hashcode of an object while it is in a set"? Do you mean at runtime? i.e.: You can't add objects to a set and then do something that will change the hashcodes of the objects while the set is in scope?

BTW - I used middlegen and hbmtojava to generate my hibernate code. It uses the primary key fields in the equals and hashcode methods by default. It sounds like I can change them if I need to.

Thanks -


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.