-->
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: play with set
PostPosted: Thu Apr 15, 2004 12:48 am 
Regular
Regular

Joined: Wed Dec 03, 2003 9:41 pm
Posts: 87
I used Set to store many-to-one association, but I happened a boring problem. Although it is a j2se Set problem, it occurs in using hibernate.
My business logic is: Add a new detail Item to parent, save it to database. If any dababase exception(HibernateException) orrur, break the association.
So my code is like this:
Code:
Parent class:
void addChild(Child child) throws Exception {
this.children.add(child);
child.setParent(this);
try{
//call a DAO method to store.
}catch(HibernateException){
this.children.remove(child);
child.setParent(null);
}
}

But I found the Set:children would not remove child correctly. I think the reason is diffierent hashCode between child instance adding to Set and child instance removing from set.
Did anyone happen this problem before? Please share your solution?

Again, If I add a batch of children, then store at last. If occur HibernateException, I call method session.refresh to reload correct data from DB. Is it a good solution?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 3:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Your equals/hashCode implementation has to be such that it does not change during the object lifetime in a set. Look at http://www.hibernate.org/109.html for more discussion on your options.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 4:45 am 
Regular
Regular

Joined: Wed Dec 03, 2003 9:41 pm
Posts: 87
Yes, I know the basic principle of equals and hashCode. My hashCode method in Child class includes field of Parent. Because two new instance with id==null and other fields are both same expect Parent field. So it exactly different hashCode before association parent instance and after association parent one. I think perhaps call child.setParent() first before add child to Set will be a better way. But it is not a root way to solve problem. Because after successing insert child to DB, field id of Child will be modified. If I need remove child from Set after successing insert into DB, it will also crash.
I read the article and http://www.hibernate.org/109.166.html
point out this problem.
So I don't think maintain same hashCode during the object lifetime in a set is a easy thing. And is there any common solution aim to changing hashCode during object in a Set?


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.