title: Hibernate/Spring3: could not initialize proxy - no Session
body: <p>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. 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. 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.</p>
  
  <p>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.</p>
  
  <p>Here's my error:</p>
  
  <pre><code>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(&lt;generated&gt;)
      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)
  </code></pre>
  
  <p>Here is my servlet config for the related beans:</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName"   value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url"     value="jdbc:mysql://10.0.0.3:3306/HelloDB" /&gt;
      &lt;property name="username" value="hello" /&gt;
      &lt;property name="password" value="w0rld" /&gt;
      &lt;property name="initialSize" value="2" /&gt;
      &lt;property name="maxActive" value="5" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="annotatedClasses"&gt;
        &lt;list&gt;
          &lt;value&gt;com.saers.data.entities.User&lt;/value&gt;
        &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
          &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
          &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
          &lt;prop key="hibernate.lazy"&gt;false&lt;/prop&gt;
        &lt;/props&gt;
      &lt;/property&gt;
    &lt;/bean&gt;
  
    &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="userDAO" class="com.saers.data.dao.UserDao"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
    &lt;/bean&gt;
  
    &lt;bean id="txProxyTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"&gt;
      &lt;property name="transactionManager" ref="transactionManager"/&gt;
      &lt;property name="transactionAttributes"&gt;
        &lt;props&gt;
          &lt;prop key="add*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="update*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="delete*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
          &lt;prop key="*"&gt;PROPAGATION_SUPPORTS,readOnly&lt;/prop&gt;
        &lt;/props&gt;
      &lt;/property&gt;
    &lt;/bean&gt;
  
    &lt;bean id="userService" parent="txProxyTemplate"&gt;
      &lt;property name="target"&gt;
        &lt;bean class="com.saers.business.UserServiceImpl"&gt;
          &lt;property name="userDao" ref="userDAO"/&gt;
        &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="proxyInterfaces" value="com.saers.business.UserService"/&gt;
    &lt;/bean&gt;
  
    &lt;bean name="/" class="com.saers.view.web.controller.HelloWorldController"&gt;
      &lt;property name="userService" ref="userService"/&gt; 
    &lt;/bean&gt;
  </code></pre>
  
  <p>Here is my User class:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>And here is my UserDao:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>Here's my UserService interface</p>
  
  <pre><code>public interface UserService {
  
      public User lookupUser(String webUserId);
      public void setUserDao(UserDao userDao);
  }
  </code></pre>
  
  <p>and its implementation:</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>And finally, here's my HelloWorldController:</p>
  
  <pre><code>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;
    }
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Cheers</p>
  
  <pre><code>Nik
  </code></pre>