views:

65

answers:

1

While trying to insert a new entry to a Many TO Many associated table , i am getting this error: Could not synchronize database state with session I can understand that this is something deals with getSession() & session.close()

But i cant able to figure it out exactly. For each transaction i am creating a new session. But i close all the sessions at the user logout. i.e: Creating a hibernate session & binds it with HttpSession. Then i destroys it in the user logout.

Some times after working on this issue, i get a different object with the same identifier value was already associated with the session: error.

All this errors are coming because of not properly opening & closing sessions.

What method you prefer to open and close sessions. My project was creating a web-app. Every single moves in that web-app is deals with DB.

Any suggestions!!!

+1  A: 

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

Hibernate keeps track of the mapped objects worked on within a session so it can detect any changes made to these objects and write them back to the database when the transaction is committed. If you were allowed to add different objects with the same database identifier object to a session, it would be ambiguous which object's state should be written.

Working with objects from the hibernate reference manual explains this (and ways to resolve it) in detail. Of particular interest is the section titled "Modifying detached objects", but will need want to read the introduction too to understand the terms used.

meriton