-->
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: Problems storing transient objects in a Set
PostPosted: Tue Nov 11, 2003 8:41 pm 
Newbie

Joined: Tue Nov 11, 2003 8:24 pm
Posts: 4
Location: Singapore
Hi,

I have difficulties in adding transient objects into a Set... can someone shed some light on what I'm missing. :)

To begin with, my transient objects have the same null identifier (please correct me if I'm wrong in doing so). Consequently these transient objects are deemed to be equal by a Set. Hence there's no way for me to add more than 1 transient objects to the same Set as only the first addition will be effective.

The last code example in section 8.2 of the Hibernate docs is a good illustration. How do I add 2 Children to the Parent at one go? My code fragment below manages to add only 1 Children:
Code:
Parent p = (Parent)session.load(Parent.class, pid);
Child c1 = new Child("Tommy");
Child c2 = new Child("Jimmy");
p.addChild(c1);
p.addChild(c2);
session.save(c);
session.flush();


Thanks in advance! :)

--
chyun


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 8:29 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
That's not a Hibernate-specific issue: If you want to put more than one new domain object into a HashSet or HashMap, you need to implement their equals/hashCode accordingly: Two new domain objects are never equal, just two *persisted* domain objects with the same non-null id are. Or don't override equals/hashCode at all.

Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2003 11:27 am 
Newbie

Joined: Tue Nov 11, 2003 8:24 pm
Posts: 4
Location: Singapore
Hi jhoeller,

Thanks for your reply.

I'm using hbm2java to generate the model classes, and both equals() and hashCode() methods are generated automatically. I'll see if I can turn that off.

Thanks. :)

--
chyun


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.