org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:564)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:552)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:544)
解决办法:[转自[url]http://forum.springsource.org/showthread.php?t=27587[/url]]
====Cause====
The program started two or more transactions while we expected only one. It is a fact caused by incorrect transaction AOP configuration in Spring context.xml
Our default transaction AOP config is:
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="move*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
This configuration means: all methods start with "add", "delete", "update", "move", "save" require transaction. all other methods support transaction.
for example, method login() calls load() and update(). both login() and load() do not require transaction, but support it. update() require transaction.
However, Both load() and update() will start new transaction. But we expected load() is out of transaction of at the same transaction with update().
====Fixing====
Declare the new method as REQUIRED.
# declare load*, find* and all other persistence query method as REQUIRED.
# declare All APIs as REQUIRED, as long as it called persistence CUD methods.
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:293)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:564)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:552)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:544)
解决办法:[转自[url]http://forum.springsource.org/showthread.php?t=27587[/url]]
====Cause====
The program started two or more transactions while we expected only one. It is a fact caused by incorrect transaction AOP configuration in Spring context.xml
Our default transaction AOP config is:
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="move*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
This configuration means: all methods start with "add", "delete", "update", "move", "save" require transaction. all other methods support transaction.
for example, method login() calls load() and update(). both login() and load() do not require transaction, but support it. update() require transaction.
However, Both load() and update() will start new transaction. But we expected load() is out of transaction of at the same transaction with update().
====Fixing====
Declare the new method as REQUIRED.
# declare load*, find* and all other persistence query method as REQUIRED.
# declare All APIs as REQUIRED, as long as it called persistence CUD methods.
本文详细探讨了Hibernate中出现尝试将集合与两个打开会话关联的异常问题,并提供了具体的解决方案。该异常通常由Spring环境中不正确的事务AOP配置引起。文章还介绍了如何正确配置Spring事务以避免此类问题。
10万+

被折叠的 条评论
为什么被折叠?



