在我的应用程序(服务器)中,我使用“ sessionFactory.openSession()
”在请求处理的开头创建了一个Hibernate会话。我在其他类中检索到该对象进行了一些更改,然后执行saveOrUpdate
,如下所示:
Session session = sessionFactory.getCurrentSession();
Transaction dbTransaction = session.getTransaction();
dbTransaction.begin();
session.saveOrUpdate(entity);
dbTransaction.commit()
我还在配置文件中设置了以下属性。
<property name="hibernate.current_session_context_class">thread</property>
但是,当我执行
session.saveOrUpdate(entity)
时,出现以下异常。我认为以下异常的原因是“ getCurrentSession()
”未获得使用“ sessionFactory.openSession()
”打开的会话,但我无法理解为什么它没有获得我使用“ sessionFactory.openSession()
打开的会话”当服务器中的请求处理开始时。错误堆栈跟踪:
org.hibernate.HibernateException: Illegal attempt to associate a collection with
two open sessions at org.hibernate.collection.internal.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:638)
at org.hibernate.event.internal.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:65)
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:121)
at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:82)
at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:76)
at org.hibernate.event.internal.AbstractVisitor.process(AbstractVisitor.java:143)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:241)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:109)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:735)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:727)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:723)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy24.saveOrUpdate(Unknown Source)
at org.app.purchase.utils.PurchaseUtils.saveOrUpdateTransactionNew(PurchaseUtils.java:46)
at org.app.purchase.model.Transaction.createRefundTransaction(Transaction.java:422)
at org.app.purchase.application.RefundTransaction.refundTransaction(RefundTransaction.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:449)
at org.restlet.resource.ServerResource.get(ServerResource.java:645)
at org.restlet.resource.ServerResource.doHandle(ServerResource.java:527)
at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:587)
at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:299)
at org.restlet.resource.ServerResource.handle(ServerResource.java:846)
at org.restlet.resource.Finder.handle(Finder.java:510)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Router.doHandle(Router.java:497)
at org.restlet.routing.Router.handle(Router.java:737)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:151)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:111)
at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:72)
at org.restlet.Application.handle(Application.java:388)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Router.doHandle(Router.java:497)
at org.restlet.routing.Router.handle(Router.java:737)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Router.doHandle(Router.java:497)
at org.restlet.routing.Router.handle(Router.java:737)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:151)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.routing.Filter.doHandle(Filter.java:156)
at org.restlet.routing.Filter.handle(Filter.java:203)
at org.restlet.engine.ChainHelper.handle(ChainHelper.java:111)
at org.restlet.Component.handle(Component.java:388)
at org.restlet.Server.handle(Server.java:488)
at org.restlet.engine.http.connector.BaseServerHelper.handle(BaseServerHelper.java:158)
at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:167)
at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
最佳答案
调用openSession()
不会将所得的Session对象与Hibernate的当前会话上下文相关联。但是,如果您调用getCurrentSession()
并且不存在任何会话,则Hibernate将打开一个新会话并将其与上下文关联。
换句话说,您需要将对openSession()
的调用替换为对getCurrentSession()
的调用。