简体   繁体   English

Hibernate / Spring3:无法初始化代理 - 没有Session

[英]Hibernate/Spring3: could not initialize proxy - no Session

I'm new at Spring, so I put Spring 3.0M3 on a Tomcat server on my Mac, made an Eclipse project, completed a Hello World and then wanted to make an object persist with Hibernate. 我是Spring的新手,所以我将Spring 3.0M3放在我的Mac上的Tomcat服务器上,创建了一个Eclipse项目,完成了一个Hello World,然后想要使用Hibernate来保持一个对象。 I've made a User table on my MySQL server, made a User object with all getters and setters (I really wish Java would take a queue from Objective-C here and add dynamic properties. Too much code clutter with generated property-code), made a UserDao object to look up and save a User, and made the bean config. 我在我的MySQL服务器上创建了一个User表,用一个getter和setter创建了一个User对象(我真的希望Java在这里从Objective-C获取队列并添加动态属性。太多的代码混乱了生成的属性代码) ,创建一个UserDao对象来查找并保存用户,并进行bean配置。 It goes fairly well... except for that the session isn't initialized. 它相当不错......除了会话没有初始化。 Why is this? 为什么是这样? I can access the database just fine from this computer using those credentials. 我可以使用这些凭据从这台计算机上访问数据库。

I realize that this is probably just normal beginner stuff, but all I've found on the error while googling has been people that are loosing the sessions mid-way when transitioning from Hibernate 2 to 3. I'm not transitioning, and as far as I can tell the session is never made. 我意识到这可能只是正常的初学者的东西,但我在谷歌搜索时发现的所有错误都是人们在从Hibernate 2转换到3时中途失去了会话。我没有过渡到目前为止因为我可以告诉会议永远不会。

Here's my error: 这是我的错误:

SEVERE: Servlet.service() for servlet HelloApp threw exception
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
    at com.saers.data.entities.User$$EnhancerByCGLIB$$c2f16afd.getName(<generated>)
    at com.saers.view.web.controller.HelloWorldController.handleRequestInternal(HelloWorldController.java:22)
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:525)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    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:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:637)

Here is my servlet config for the related beans: 这是我的相关bean的servlet配置:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"   value="com.mysql.jdbc.Driver" />
    <property name="url"     value="jdbc:mysql://10.0.0.3:3306/HelloDB" />
    <property name="username" value="hello" />
    <property name="password" value="w0rld" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
      <list>
        <value>com.saers.data.entities.User</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.lazy">false</prop>
      </props>
    </property>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <bean id="userDAO" class="com.saers.data.dao.UserDao">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <bean id="txProxyTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
      <props>
        <prop key="add*">PROPAGATION_REQUIRED</prop>
        <prop key="update*">PROPAGATION_REQUIRED</prop>
        <prop key="delete*">PROPAGATION_REQUIRED</prop>
        <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
      </props>
    </property>
  </bean>

  <bean id="userService" parent="txProxyTemplate">
    <property name="target">
      <bean class="com.saers.business.UserServiceImpl">
        <property name="userDao" ref="userDAO"/>
      </bean>
    </property>
    <property name="proxyInterfaces" value="com.saers.business.UserService"/>
  </bean>

  <bean name="/" class="com.saers.view.web.controller.HelloWorldController">
    <property name="userService" ref="userService"/> 
  </bean>

Here is my User class: 这是我的User类:

package com.saers.data.entities;

import java.util.Date;
import java.io.Serializable;

import javax.persistence.*;

@Entity
@Table(name = "User")
public class User implements Serializable {

    private static final long serialVersionUID = -6123654414341191669L;

    @Id
    @Column(name = "WebUserId")
    private String WebUserId;

    @Column(name = "Name")
    private String Name;
    /**
     * @return the webUserId
     */
    public synchronized String getWebUserId() {
        return WebUserId;
    }
    /**
     * @param webUserId the webUserId to set
     */
    public synchronized void setWebUserId(String webUserId) {
        WebUserId = webUserId;
    }
    /**
     * @return the name
     */
    public synchronized String getName() {
        return Name;
    }
    /**
     * @param name the name to set
     */
    public synchronized void setName(String name) {
        Name = name;
    }
}

And here is my UserDao: 这是我的UserDao:

package com.saers.data.dao;

import java.util.List;

import com.saers.data.entities.User;

import org.springframework.orm.hibernate3.support.*;


public class UserDao extends HibernateDaoSupport {

    public void saveUser(User user) {
        getHibernateTemplate().saveOrUpdate(user);
    }

    public User lookupUser(String WebUserId) {
        User user = getHibernateTemplate().load(User.class, WebUserId);
        return user;
        return user;
    }
}

Here's my UserService interface 这是我的UserService界面

public interface UserService {

    public User lookupUser(String webUserId);
    public void setUserDao(UserDao userDao);
}

and its implementation: 及其实施:

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User lookupUser(String webUserId) {

        return userDao.lookupUser(webUserId);
    }
}

And finally, here's my HelloWorldController: 最后,这是我的HelloWorldController:

package com.saers.view.web.controller;

import com.saers.data.entities.User;
import com.saers.business.UserService;

import org.springframework.web.servlet.mvc.*;
import org.springframework.web.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.*;



public class HelloWorldController extends AbstractController {

  protected final Log logger = LogFactory.getLog(getClass());

  @Override
  public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

    logger.info("Get bean");
    User user = userService.lookupUser("helloUser");
    logger.info("Found out that this user was last changed " + user.getName());
    logger.info("Return View");
    ModelAndView mv = new ModelAndView("HelloWorld.jsp", "user", user);
    return mv;
  }

  private UserService userService = null;
  public void setUserService(UserService userService) {
      this.userService = userService;
  }

}

I hope you have some good tips I can use :-) If there's anything else in the code you feel that I'm doing the wrong/non-spring way, I would love to hear about that as well. 我希望你有一些我可以使用的好技巧:-)如果代码中还有其他任何东西你觉得我做错了/非弹簧的方式,我也很想听到这个。

Cheers 干杯

Nik

Your stack trace does not match up with the sample code you posted for UserDao . 您的堆栈跟踪与您为UserDao发布的示例代码不匹配。 The trace says that you're calling HibernateTemplate.initialize() from UserDao.lookupUser() , but your sample code is doing no such thing (as well it shouldn't, initialize() should not be called here). 跟踪说你正在从UserDao.lookupUser()调用HibernateTemplate.initialize() ,但是你的示例代码没有这样做(也不应该,不应该在这里调用initialize() )。

Edit: OK, with the new stack trace, it seems the problem is simply that you're calling hibernateTemplate.load() , which should only be used in specific circumstances (and this isn't one of them). 编辑:好的,使用新的堆栈跟踪,似乎问题只是你正在调用hibernateTemplate.load() ,它只应该在特定情况下使用(这不是其中之一)。 You should probably be calling hibernateTemplate.get() . 你可能应该调用hibernateTemplate.get()

When you do the dao call, you "load" the object. 当您执行dao调用时,您将“加载”该对象。 Load does not hit the database, it will return a proxy onto the object so that the database is only hit when absolutely necessary. 加载不会访问数据库,它会将代理返回到对象上,这样只有在绝对必要时才会命中数据库。

You did not start a transaction before the load - the load works because you have non transactional reads turned on (it's on by default). 您没有在加载之前启动事务 - 加载有效,因为您打开了非事务性读取(默认情况下它处于启用状态)。 It doesn't say, sorry, no transaction (it basically creates one for you to do the read, which ends immediately). 它没有说,对不起,没有交易(它基本上为你创建一个读取,立即结束)。

Thus when you do the getName when it actually has to hit the database it tells you "no session" because the session which did load that object is closed (it was closed immediately). 因此,当你在实际必须访问数据库时执行getName时,它会告诉你“没有会话”,因为加载该对象的会话已关闭(它立即关闭)。

What you need to do to fix it is start the transaction at the beginning of the servlet call and end it at the end - that way when you do the getName the session will still be available. 你需要做的是修复它是在servlet调用开始时启动事务并在结束时结束 - 这样当你执行getName时,会话仍然可用。

There's probably something in spring which will handle this plumbing for you. 春天可能会有一些东西可以为你处理这个管道。

暂无
暂无

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

相关问题 Hibernate无法初始化代理 - 没有Session - Hibernate could not initialize proxy - no Session Spring DATA JPA + Hibernate-无法初始化代理-修复后无会话: - Spring DATA JPA + Hibernate - could not initialize proxy - no Session after fix: 无法初始化代理-没有会话(春季休眠一对一) - could not initialize proxy - no Session (Spring-Hibernate-one to one) Spring JPA - org.hibernate.LazyInitializationException:无法初始化代理 - 无 Z71AB3B3AE294B3ABDE46 - Spring JPA - org.hibernate.LazyInitializationException: 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 错误:休眠无法初始化代理-没有会话 - Error: Hibernate could not initialize proxy - no Session Hibernate 4 + Spring Data CrudRepository,CLI应用程序:无法延迟初始化集合:无法初始化代理-没有会话 - Hibernate 4 + Spring Data CrudRepository, CLI application: failed to lazily initialize a collection: could not initialize proxy - no Session Hibernate 延迟加载不适用于 Spring 启动 =&gt; 无法延迟初始化角色集合无法初始化代理 - 没有 Session - Hibernate Lazy loading not working with Spring Boot => failed to lazily initialize a collection of role could not initialize proxy - no Session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM