aspose file tools
The moose likes Object Relational Mapping and the fly likes a different object with the same identifier value was already associated with the session Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "a different object with the same identifier value was already associated with the session" Watch "a different object with the same identifier value was already associated with the session" New topic
Author

a different object with the same identifier value was already associated with the session

Kumar Sushil
Greenhorn

Joined: Jan 31, 2006
Posts: 29
Hello all,

I am doing following activity in my applicatin -
1. Fetch object using getHibernateTemplate().get(String entityName, Object obj).
2. If in step #1, object is present in database, then call getHibernateTemplate().delete(String entityName, Object id).
3. Fetch object using getHibernateTemplate().get(String entityName, Object obj), so it will give null.
4. Populate object with same property having by object in step #1.
5. Call getHibernateTemplate().saveOrUpdate(String entityName, Object obj). passed object was populated in step #4.

On executing step #5, I am getting exception- a different object with the same identifier value was already associated with the session

Please note that all steps from #1 to #5 are part of single transaction.
Could you please advice me to handle this exception.

Thanks in advance.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1282

What order are your statements executing in? Are you printing them out? Often times deletes will be executed after inserts. You can try flushing the session after the delete to make sure it executes first. You can also call evict to make sure that object is not in the session, before creating a new. Use of hibernate template is now discouraged. See the Spring documentation for the preferred approach.
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/orm.html#orm-hibernate


[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: a different object with the same identifier value was already associated with the session
 
Similar Threads
JPA - EntityExistsException with Composite Id
Issue with saving pojo in a transactional wrapper
While saving giving session problem
Non Unique object Exception
NonUniqueObjectException in Hibernate