Illegal attempt to associate a collection with two open sessions

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)

解决办法:[转自 http://forum.springsource.org/showthread.php?t=27587]
====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.

你可能感兴趣的:(java,spring,AOP,Hibernate,PHP)