Hibernate Session关闭问题!

发布于 2024-09-07 14:45:44 字数 8201 浏览 1 评论 0原文

下面的函数是我的 Web 应用程序中按钮的 actionListener,我正在从数据库的表中删除旧的选定行并将新行插入到数据库中。

public void getSelectedExemptionItems(ActionEvent ae) {
        Session hibernateSession;
        Session hibernate2Session;
        selectedExemptions = new ArrayList<ExemptionBean>();
        for (ExemptionBean eBean : exemBean) { // iterating over the list of ExemptionBean class
            if (selectedExemptionIDs.get(eBean.getExemptionID()).booleanValue()) {
                selectedExemptions.add(eBean); // adding every ExemptionBean that is selected.
            }
        }
        // getting importane session variables
        SessionBean1 theSessionBean = (SessionBean1) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("SessionBean1");
        JAdmApplication admApplication = theSessionBean.getAdmApplication();
        long admAppID = admApplication.getJAdmApplicationId();
//        ArrayList<JAdmAppExemption> admAppExemption = (ArrayList<JAdmAppExemption>)theSessionBean.getAdmAppExemption();

        JAdmAppExemption admAppExem = new JAdmAppExemption();

        // try catch : delete all previously chosen exemptions from database by logged-in user.
        try {
            hibernateSession = HibernateUtil.getAdmSessionFactory().getCurrentSession();
            Transaction t = hibernateSession.beginTransaction();
            t.begin();
            String sequel = "DELETE FROM JAdmAppExemption WHERE createdby = ?";
            Query q = hibernateSession.createQuery(sequel);
            q.setParameter(0, theSessionBean.getPortalUserId());

        } catch (Exception e){
            System.out.println("Row could not be deledted from database!!");
        }

//         adding all selected exemptions to the database

        for (ExemptionBean e : selectedExemptions) {
            admAppExem.setAdClientId(1000001);
            admAppExem.setAdOrgId(1000001);
            admAppExem.setCreated(getTimestamp());
            admAppExem.setCreatedby(theSessionBean.getPortalUserId());
            admAppExem.setIsactive('Y');
            admAppExem.setJAdmAppExemptionId(getJAdmAppExemptionIdSequence());
            admAppExem.setJAdmApplicationId(admAppID);
            admAppExem.setJAdmExemptionId(e.getExemptionID());
            admAppExem.setUpdated(getTimestamp());
            admAppExem.setUpdatedby(theSessionBean.getPortalUserId());

            hibernate2Session = HibernateUtil.getAdmSessionFactory().getCurrentSession(); // session is opened here!
            Transaction tx = hibernate2Session.beginTransaction();
            tx.begin();
            try {
                hibernate2Session.beginTransaction();
                hibernate2Session.save(admAppExem); // is it save or saveOrUpdate??
                hibernate2Session.getTransaction().commit();
                updateJAdmAppExemptionIdSequence();

            } catch (Throwable ex){
                if (hibernate2Session.getTransaction().isActive()) {
                    hibernate2Session.getTransaction().rollback();
                }
                ex.printStackTrace();
            }
        }
    }

我不断收到异常:会话已关闭!有人可以解释一下如何在休眠中关闭会话(自动?)以及如何使用它们。

这是异常的详细堆栈跟踪:

javax.faces.FacesException: org.hibernate.SessionException: Session is closed!
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:90)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
        at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:132)
        at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:74)
        at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:31)
        at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:24)
        at com.icesoft.faces.webapp.http.servlet.BasicAdaptingServlet.service(BasicAdaptingServlet.java:16)
        at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
        at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:53)
        at com.icesoft.faces.webapp.http.servlet.SessionVerifier.service(SessionVerifier.java:26)
        at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
        at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:131)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
        at java.lang.Thread.run(Thread.java:619)
Caused by: javax.faces.el.EvaluationException: org.hibernate.SessionException: Session is closed!
        at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
        at javax.faces.component.UICommand.broadcast(UICommand.java:380)
        at com.icesoft.faces.component.panelseries.UISeries$RowEvent.broadcast(UISeries.java:617)
        at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:285)
        at com.icesoft.faces.component.paneltabset.PanelTabSet.broadcast(PanelTabSet.java:320)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
        ... 27 more
Caused by: org.hibernate.SessionException: Session is closed!
        at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
        at sun.reflect.GeneratedMethodAccessor2480.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
        at $Proxy233.beginTransaction(Unknown Source)
        at univportal.AdmissionApplication.Application.getSelectedExemptionItems(Application.java:1595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.el.parser.AstValue.invoke(AstValue.java:172)
        at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
        at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
        ... 34 more

谢谢,

The function below is the actionListener for a button I have in my web application, I am deleting the old chosen rows from a table in a database and inserting the new ones to the database.

public void getSelectedExemptionItems(ActionEvent ae) {
        Session hibernateSession;
        Session hibernate2Session;
        selectedExemptions = new ArrayList<ExemptionBean>();
        for (ExemptionBean eBean : exemBean) { // iterating over the list of ExemptionBean class
            if (selectedExemptionIDs.get(eBean.getExemptionID()).booleanValue()) {
                selectedExemptions.add(eBean); // adding every ExemptionBean that is selected.
            }
        }
        // getting importane session variables
        SessionBean1 theSessionBean = (SessionBean1) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("SessionBean1");
        JAdmApplication admApplication = theSessionBean.getAdmApplication();
        long admAppID = admApplication.getJAdmApplicationId();
//        ArrayList<JAdmAppExemption> admAppExemption = (ArrayList<JAdmAppExemption>)theSessionBean.getAdmAppExemption();

        JAdmAppExemption admAppExem = new JAdmAppExemption();

        // try catch : delete all previously chosen exemptions from database by logged-in user.
        try {
            hibernateSession = HibernateUtil.getAdmSessionFactory().getCurrentSession();
            Transaction t = hibernateSession.beginTransaction();
            t.begin();
            String sequel = "DELETE FROM JAdmAppExemption WHERE createdby = ?";
            Query q = hibernateSession.createQuery(sequel);
            q.setParameter(0, theSessionBean.getPortalUserId());

        } catch (Exception e){
            System.out.println("Row could not be deledted from database!!");
        }

//         adding all selected exemptions to the database

        for (ExemptionBean e : selectedExemptions) {
            admAppExem.setAdClientId(1000001);
            admAppExem.setAdOrgId(1000001);
            admAppExem.setCreated(getTimestamp());
            admAppExem.setCreatedby(theSessionBean.getPortalUserId());
            admAppExem.setIsactive('Y');
            admAppExem.setJAdmAppExemptionId(getJAdmAppExemptionIdSequence());
            admAppExem.setJAdmApplicationId(admAppID);
            admAppExem.setJAdmExemptionId(e.getExemptionID());
            admAppExem.setUpdated(getTimestamp());
            admAppExem.setUpdatedby(theSessionBean.getPortalUserId());

            hibernate2Session = HibernateUtil.getAdmSessionFactory().getCurrentSession(); // session is opened here!
            Transaction tx = hibernate2Session.beginTransaction();
            tx.begin();
            try {
                hibernate2Session.beginTransaction();
                hibernate2Session.save(admAppExem); // is it save or saveOrUpdate??
                hibernate2Session.getTransaction().commit();
                updateJAdmAppExemptionIdSequence();

            } catch (Throwable ex){
                if (hibernate2Session.getTransaction().isActive()) {
                    hibernate2Session.getTransaction().rollback();
                }
                ex.printStackTrace();
            }
        }
    }

I keep getting an exception : Session is closed !! can someone please explain how sessions are closed in hibernate (automatically??) and how to work with them..

here is the detailed stack trace of the exception:

javax.faces.FacesException: org.hibernate.SessionException: Session is closed!
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:90)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
        at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:132)
        at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:74)
        at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:31)
        at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:24)
        at com.icesoft.faces.webapp.http.servlet.BasicAdaptingServlet.service(BasicAdaptingServlet.java:16)
        at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
        at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:53)
        at com.icesoft.faces.webapp.http.servlet.SessionVerifier.service(SessionVerifier.java:26)
        at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
        at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:131)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
        at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
        at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
        at java.lang.Thread.run(Thread.java:619)
Caused by: javax.faces.el.EvaluationException: org.hibernate.SessionException: Session is closed!
        at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
        at javax.faces.component.UICommand.broadcast(UICommand.java:380)
        at com.icesoft.faces.component.panelseries.UISeries$RowEvent.broadcast(UISeries.java:617)
        at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:285)
        at com.icesoft.faces.component.paneltabset.PanelTabSet.broadcast(PanelTabSet.java:320)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
        ... 27 more
Caused by: org.hibernate.SessionException: Session is closed!
        at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
        at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
        at sun.reflect.GeneratedMethodAccessor2480.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
        at $Proxy233.beginTransaction(Unknown Source)
        at univportal.AdmissionApplication.Application.getSelectedExemptionItems(Application.java:1595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.el.parser.AstValue.invoke(AstValue.java:172)
        at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
        at com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
        ... 34 more

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

熟人话多 2024-09-14 14:45:45

这是我对这个问题找到的简明答案 这里如果有人在 Hibernate 中遇到这个问题。显然这很简单!

在 Hibernate 中使用托管会话来简化单元测试 06nov06

如果您曾经尝试在 Hibernate 中重用会话,您可能会遇到此异常...

org.hibernate.SessionException: Session is closed!
   at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
   at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.beginTransaction(Unknown Source)
   ....

原因是 Hibernate 使用线程托管会话。通过这种类型的会话管理,Hibernate 可以为您管理会话。当您第一次尝试使用会话时,Hibernate 将创建一个会话并将其附加到您的本地线程。当您在会话中提交事务时,Hibernate 将自动关闭会话,这意味着它无法重用。

要解决此问题,最好的选择是使用托管会话。通过托管会话,您可以完全控制创建、刷新、提交和关闭会话。方法如下。

hibernate.cfg.xml 中,将属性 current_session_context_class 更改为 托管

要创建会话并在该会话中启动事务,请执行以下操作...

org.hibernate.classic.Session session = HibernateUtil.getSessionFactory().openSession();
   session.setFlushMode(FlushMode.MANUAL);
   ManagedSessionContext.bind(session);
   session.beginTransaction();

提交事务< /code> 在 session 中执行此操作...

ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
   session.flush();
   session.getTransaction().commit();
   session.close();

为了在单元测试中使用此代码,我创建了这个基本单元测试类,我的所有单元测试都扩展了它。每当我想创建一个新的会话事务时,我都会调用createNewSessionAndTransaction()方法。为了提交会话的事务,我调用方法 commitTransaction()

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.context.ManagedSessionContext;
import util.HibernateUtil;
import junit.framework.TestCase;

/**
 * Abstract unit test with helper methods for managed session control
 */
public abstract class ManagedSessionUnitTest extends TestCase {

   /**
    * Create a new Session.
    *
    * For this method to work, the application managed session strategy has to
    * be enabled. This basically means that the life of a session is controlled
    * by you and and not by Hibernate.
    *
    * To enable the application managed session strategy set the property
    * hibernate.current_session_context_class to "managed".
    *
    * Within this method we create a new session and set the flush mode to
    * MANUAL. This ensures that we have full control over when the session is
    * flushed to the database.
    */
   protected org.hibernate.Session createNewSession() {
      org.hibernate.classic.Session session = HibernateUtil.getSessionFactory().openSession();
      session.setFlushMode(FlushMode.MANUAL);
      ManagedSessionContext.bind(session);
      return (org.hibernate.Session) session;
   }

   /**
    * Start a new Transaction in the given session
    * @param session The session to create the transaction in
    */
   protected void startNewTransaction(Session session) {
      session.beginTransaction();
   }

   /**
    * Shortcut method that creates a new session and begins a transaction in it
    * @return A new session with a transaction started
    */
   protected org.hibernate.Session createNewSessionAndTransaction() {
      Session session = createNewSession();
      startNewTransaction(session);
      return session;
   }

   /**
    * Commit the transaction within the given session. This method unbinds
    * the session from the session context (ManagedSessionContext), flushes
    * the session, commmits the session and then closes the session
    * @param session The session with the transaction to commit
    */
   protected void commitTransaction(Session session) {
      ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
      session.flush();
      session.getTransaction().commit();
      session.close();
   }

}

Here is a concise answer I found to this question here if anyone encounters this problem in Hibernate. Apparently it is simple!

using managed sessions in hibernate to ease unit testing 06nov06

If you’ve ever tried to reuse a session in Hibernate you may have come across this exception…

org.hibernate.SessionException: Session is closed!
   at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
   at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1319)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.beginTransaction(Unknown Source)
   ....

The reason for this is that Hibernate is using thread managed sessions. With this type of session management Hibernate manages the session for you. When you first attempt to use a session Hibernate will create one and attach it to your local thread. When you commit the transaction in the session Hibernate will automatically close the session meaning it can’t be reused.

To get around this problem the best option is to use managed sessions. With managed sessions you’re in full control of creating, flushing, commiting, and closing sessions. Here’s how.

In your hibernate.cfg.xml change the property current_session_context_class to managed.

To create a session and start a transaction in that session do this…

org.hibernate.classic.Session session = HibernateUtil.getSessionFactory().openSession();
   session.setFlushMode(FlushMode.MANUAL);
   ManagedSessionContext.bind(session);
   session.beginTransaction();

To commit a transaction in the session do this…

ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
   session.flush();
   session.getTransaction().commit();
   session.close();

To use this code in unit tests I created this base unit test class that all my unit tests extend. Whenever I want to create a new session or transaction I call the method createNewSessionAndTransaction(). To commit the session’s transaction I call the method commitTransaction().

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.context.ManagedSessionContext;
import util.HibernateUtil;
import junit.framework.TestCase;

/**
 * Abstract unit test with helper methods for managed session control
 */
public abstract class ManagedSessionUnitTest extends TestCase {

   /**
    * Create a new Session.
    *
    * For this method to work, the application managed session strategy has to
    * be enabled. This basically means that the life of a session is controlled
    * by you and and not by Hibernate.
    *
    * To enable the application managed session strategy set the property
    * hibernate.current_session_context_class to "managed".
    *
    * Within this method we create a new session and set the flush mode to
    * MANUAL. This ensures that we have full control over when the session is
    * flushed to the database.
    */
   protected org.hibernate.Session createNewSession() {
      org.hibernate.classic.Session session = HibernateUtil.getSessionFactory().openSession();
      session.setFlushMode(FlushMode.MANUAL);
      ManagedSessionContext.bind(session);
      return (org.hibernate.Session) session;
   }

   /**
    * Start a new Transaction in the given session
    * @param session The session to create the transaction in
    */
   protected void startNewTransaction(Session session) {
      session.beginTransaction();
   }

   /**
    * Shortcut method that creates a new session and begins a transaction in it
    * @return A new session with a transaction started
    */
   protected org.hibernate.Session createNewSessionAndTransaction() {
      Session session = createNewSession();
      startNewTransaction(session);
      return session;
   }

   /**
    * Commit the transaction within the given session. This method unbinds
    * the session from the session context (ManagedSessionContext), flushes
    * the session, commmits the session and then closes the session
    * @param session The session with the transaction to commit
    */
   protected void commitTransaction(Session session) {
      ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
      session.flush();
      session.getTransaction().commit();
      session.close();
   }

}
美人如玉 2024-09-14 14:45:45

您在会话范围的托管 bean 中使用延迟获取,而 Hibernate 会话显然配置为基于请求。

长话短说:了解 Hibernate 中的延迟获取

You're using lazy fetching in a session scoped managed bean while the Hibernate session is apparently configured to be request based.

Long story short: Understanding Lazy Fetching in Hibernate.

白昼 2024-09-14 14:45:45

尝试将其添加到您的控制器方法中:

@Transactional(readOnly = true)

Try adding this to your controller method:

@Transactional(readOnly = true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文