0

在我的应用程序(服务器)中,我在请求处理开始时使用“ 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()当请求处理在服务器中开始时它没有获得我使用“”打开的 currentSession .

错误堆栈跟踪:

    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)
4

1 回答 1

0

调用openSession()不会将生成的 Session 对象与 Hibernate 的当前会话上下文相关联。但是,如果您调用getCurrentSession()并且不存在会话,那么 Hibernate 将打开一个新会话并将其与上下文相关联。

换句话说,您需要将您的呼叫替换为openSession()对 的呼叫getCurrentSession()

于 2013-10-27T11:44:29.657 回答