Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kumar Sushil
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic