休眠:无法与会话同步数据库状态 [英] Hibernate: Could not synchronize database state with session

查看:103
本文介绍了休眠:无法与会话同步数据库状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


$ b同一个标识符值已经被关联了


$ b

在尝试向多关联表中插入新条目时,出现此错误:无法使数据库状态与session
我可以理解这是与getSession()& session.close()



但我无法完全弄清楚。对于每笔交易,我都会创建一个新的会话。
但是我关闭了用户注销时的所有会话。即:创建一个休眠会话&将它与HttpSession绑定。然后我在用户注销中销毁它。



在处理完这个问题后,我得到具有相同标识符值的不同对象与会话相关:错误。



所有这些错误都是由于没有正确打开&关闭会议。



您更喜欢打开和关闭会话的方法。我的项目正在创建一个Web应用程序。
该web应用程序中的每一个动作都与数据库有关。

任何建议!!!

解决方案


具有相同标识符值的不同对象已与会话相关联


Hibernate跟踪在因此它可以检测对这些对象所做的任何更改,并在事务提交时将它们写回数据库。如果您允许将具有相同数据库标识符对象的不同对象添加到会话中,那么应该写入哪个对象的状态是不明确的。


$ b $ p

使用hibernate参考手册中的对象解释了这一点(以及解决方法它)详细。特别感兴趣的是标题为修改分离对象的部分,但为了理解所使用的术语,还需要阅读引言。


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

解决方案

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.

这篇关于休眠:无法与会话同步数据库状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆