-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to check if object is in session conflicts with equals()
PostPosted: Mon Jul 06, 2009 4:01 am 
Newbie

Joined: Mon Jul 06, 2009 3:28 am
Posts: 2
Hi,

Hibernate encourages to write custom equals() and hashCode() methods, not based on the id (see https://www.hibernate.org/109.html);
I can understand why, but it comes in conflict in the following scenario.

1) Suppose my DAO has a saveOrUpdate() method. As is its now, I just delegate to the hibernate's Session.saveOrUpdate().
So far, no problem.

2) suppose now a service that uses this dao. It has a method that takes a detached entity (changed) as argument (let's call this instance now instance A). The service call does first some checks and queries for checking existence, etc,. doing this, it loads also the persistent instance, whith the same Identifier as instance A.

3) the service wants to saveOrUpdate the instance A via the dao, but the doa gets an exception since there is 'already an object with the same identifier in the session'.

4) A solution would indeed be to use merge() instead of saveOrUpdate().

5) But, I want to use saveOrUpdate() when the object is not in the session, and use merge() when it IS in the session.

6) I thought I could use the Session.contains() method for this, but here comes the problem. Since the entity's equals() and hashCode() are implemented, using 'business fields' (as Hibernate encourages), the contains() returns always false. So, there is no way to see of an object is already in the session or not.

I would think a method like Session.contains(Class, Serializable id) would solve this, but unfortunately, this method does not exist..

Am I seeing this completely wrong, or is there a workaround for this?

Thanks!


Top
 Profile  
 
 Post subject: Re: How to check if object is in session conflicts with equals()
PostPosted: Mon Jul 06, 2009 6:09 am 
Beginner
Beginner

Joined: Wed Jun 17, 2009 9:03 pm
Posts: 31
Location: mumbai
In this case i guess you need to use some immutable properties in hashcode() and equals() method implementation rather than mutable business properties.
session.contains(Object) will work as supposed in this scenario.


Top
 Profile  
 
 Post subject: Re: How to check if object is in session conflicts with equals()
PostPosted: Mon Jul 06, 2009 6:53 am 
Newbie

Joined: Mon Jul 06, 2009 3:28 am
Posts: 2
bummer; in my case there are no immutable properties (except the id)...
Thanks for the reply anyway.. I'll keep this in mind for the future.


Top
 Profile  
 
 Post subject: Re: How to check if object is in session conflicts with equals()
PostPosted: Mon Jul 27, 2009 4:53 am 
Newbie

Joined: Sun Jul 27, 2008 10:56 am
Posts: 17
It is also one of my problems....

It is not always possible to define a unique business key using only simple entity properties, often my business keys should be formed by simple properties and some property of one or more association

Granny have you found an answer for your question?

Thanks and best regards
Gamby


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