简体   繁体   English

Hibernate无法初始化代理

[英]Hibernate could not initialize proxy

I'm getting the following exception.. 我收到以下异常。

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/assessment] threw exception [Request processing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
    at org.assessme.com.entity.User_$$_javassist_0.getFirstName(User_$$_javassist_0.java)
    at org.assessme.com.UserManagementController.data(UserManagementController.java:40)
    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.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

The problem I'm pretty sure of is in my DAO... 我很确定的问题是在我的DAO中...

public class UserDAO {

    public User getById(Long id) {
        HibernateUtil.buildSessionFactory();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        User user = (User) session.load(User.class, id);
        session.getTransaction().commit();
        return user;
    }
}

Is this the correct way to handle the transactions? 这是处理交易的正确方法吗? Should I do buildSessionFactory every time I want to return an object? 每当我想返回一个对象时,都应该执行buildSessionFactory吗? Is it better to only do it once? 只做一次更好吗?

session.beginTransaction() returns the Transaction object, you should call the commit into that instance. session.beginTransaction()返回Transaction对象,您应该将该提交调用到该实例中。 You are handling two separate transactions in each session.getTransaction() . 您正在每个session.getTransaction()中处理两个单独的事务。 Also make sure you close the session ( session.close() ) in any place. 还要确保在任何地方关闭会话( session.close() )。

http://docs.jboss.org/hibernate/orm/3.5/reference/es-ES/html/example-weblog.html http://docs.jboss.org/hibernate/orm/3.5/reference/es-ES/html/example-weblog.html

Try 尝试

HibernateUtil.getSessionFactory().beginSession(); HibernateUtil.getSessionFactory()。beginSession();

instead of ...getSessionFactory().getCurrentSession(); 而不是... getSessionFactory()。getCurrentSession();

In your case, if no session has been previously created, you're not retrieving an active session. 就您而言,如果以前未创建任何会话,则您不会检索活动会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate无法初始化代理 - 没有Session - Hibernate could not initialize proxy - no Session 无法初始化代理-休眠-Grails-多租户 - Could not initialize proxy - hibernate - grails - multi tenants 错误:休眠无法初始化代理-没有会话 - Error: Hibernate could not initialize proxy - no Session Hibernate / Spring3:无法初始化代理 - 没有Session - Hibernate/Spring3: could not initialize proxy - no Session 休眠延迟异常无法初始化代理-无会话 - hibernate lazy exception could not initialize proxy - no Session Hibernate LazyInitializationException:无法初始化代理-没有会话 - Hibernate LazyInitializationException: could not initialize proxy - no Session Hibernate中的LazyInitializationException:无法初始化代理 - 没有Session - LazyInitializationException in Hibernate : could not initialize proxy - no Session Hibernate 无法延迟初始化角色集合 无法初始化代理 - 没有 Session - Hibernate failed to lazily initialize a collection of role could not initialize proxy - no Session 休眠-无法延迟初始化角色集合:无法初始化代理-没有会话 - Hibernate - failed to lazily initialize a collection of role: could not initialize proxy - no Session 休眠问题:org.hibernate.LazyInitializationException:无法初始化代理-没有会话 - Hibernate issue: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM