Hibernate: Could not synchronize database state with session [duplicate]

独自空忆成欢 提交于 2019-12-22 05:11:55

问题


Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session

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 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.



来源:https://stackoverflow.com/questions/3820452/hibernate-could-not-synchronize-database-state-with-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!