title: org.hibernate.LazyInitializationException: could not initialize proxy - no Session. Spring + Hibernate + HSQLDB
body: <p>I have some problems with Spring + Hibernate + HSQLDB configuration.
  I receive:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  root cause
  
  org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
  org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
  org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
  org.siwoz.dao.model.Person_$$_javassist_0.toString(Person_$$_javassist_0.java)
  org.siwoz.controller.IndexController.index(IndexController.java:31)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  java.lang.reflect.Method.invoke(Method.java:483)
  org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
  org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
  org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
  org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>I've tried to use OpenSessionInViewInterceptor (for hibernate 3), but then i have:</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Resource must not be null
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  root cause
  
  java.lang.IllegalArgumentException: Resource must not be null
  org.springframework.util.Assert.notNull(Assert.java:112)
  org.springframework.transaction.support.TransactionSynchronizationUtils.unwrapResourceIfNecessary(TransactionSynchronizationUtils.java:62)
  org.springframework.transaction.support.TransactionSynchronizationManager.hasResource(TransactionSynchronizationManager.java:124)
  org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor.preHandle(OpenSessionInViewInterceptor.java:143)
  org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter.preHandle(WebRequestHandlerInterceptorAdapter.java:54)
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:891)
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
  org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  </code></pre>
  
  <p>Here is my cfg file:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource({ "classpath:db/db.properties" })
  @ComponentScan({ "my_package_name" })
  public class PersistenceConfig {
  
  @Autowired
  Environment env;
  
  @Bean
  public AnnotationSessionFactoryBean sessionFactory() {
      AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
      sessionFactory.setDataSource(restDataSource());
      sessionFactory
              .setPackagesToScan(new String[] { "my_package_name" });
      sessionFactory.setHibernateProperties(hibernateProperties());
      try {
          sessionFactory.afterPropertiesSet();
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return sessionFactory;
  }
  
  @Bean
  public HibernateTemplate hibernateTemplate() {
      return new HibernateTemplate(sessionFactory().getObject());
  }
  
  @Bean
  public DataSource restDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
      dataSource.setUrl(env.getProperty("jdbc.url"));
      dataSource.setUsername(env.getProperty("jdbc.user"));
      return dataSource;
  }
  
  @Bean
  public HibernateTransactionManager transactionManager() {
      HibernateTransactionManager txManager = new HibernateTransactionManager();
      txManager.setSessionFactory(sessionFactory().getObject());
      return txManager;
  }
  
  @Bean
  public HibernateExceptionTranslator hibernateExceptionTranslator() {
      return new HibernateExceptionTranslator();
  }
  
  Properties hibernateProperties() {
      return new Properties() {
          private static final long serialVersionUID = 8249579451140862991L;
          {
              setProperty("hibernate.connection.pool_size",
                      env.getProperty("hibernate.conn.pool"));
              setProperty("hibernate.hbm2ddl.auto",
                      env.getProperty("hibernate.hbm2ddl.auto"));
              setProperty("hibernate.dialect",
                      env.getProperty("hibernate.dialect"));
              setProperty("hibernate.globally_quoted_identifiers", "true");
              setProperty(
                      "hibernate.globally_quoted_identifiers",
                      env.getProperty("hibernate.cache.use_second_level_cache"));
              setProperty("hibernate.globally_quoted_identifiers",
                      env.getProperty("hibernate.cache.use_query_cache"));
          }
      };
  }
  }
  </code></pre>
  
  <p>I have an abstract repository, here is the piece of code:<br/></p>
  
  <pre><code>public abstract class AbstractRepository&lt;T&gt; {
  
  @Autowired
  protected HibernateTemplate hibernateTemplate;
  
  public abstract T getById(long id);
  }
  </code></pre>
  
  <p>And here is my PersonRepository:</p>
  
  <pre><code>@Repository("personRepository")
  public class PersonRepository extends AbstractRepository&lt;Person&gt; {
  
  @Override
       public Person getById(long id) {
      return hibernateTemplate.load(Person.class, id);
  }
  </code></pre>
  
  <p>I'm using only annotation config (no xmls) and loading my db scheme and data from *.sql files - I can see the scheme and data in hsql manager.<br/> Exceptions occur when I invoke getById(). I also checked my entity in sessionFactory.getObject().getAllClassMetadata()- its loaded.</p>
  
  <p>Spring - 3.1<br/>
  Hibernate - 3.6.10.Final<br/>
  Tomcat - 7<br/>
  HSQLDB - 2.3.2<br/>
  I think there is a stupid mistake, but I cannot see it.
  Any ideas? Thanks a lot!</p>
  
  <p>EDIT:<br/>
  I noticed strange errors in my Tomcat console: <br/></p>
  
  <pre><code>[ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:386) Unsucces
  sful: create table `Address` (`id` bigint not null auto_increment, `city` varcha
  r(255), `street` varchar(255), primary key (`id`))
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:387) unexpect
  ed token:  required: (
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:386) Unsucces
  sful: create table `Person` (`id` bigint not null auto_increment, `name` varchar
  (255), `pass` varchar(255), `pesel` varchar(255), `surname` varchar(255), primar
  y key (`id`))
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:387) unexpect
  ed token:  required: (
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:386) Unsucces
  sful: create table `Address` (`id` bigint not null auto_increment, `city` varcha
  r(255), `street` varchar(255), primary key (`id`))
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:387) unexpect
  ed token:  required: (
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:386) Unsucces
  sful: create table `Person` (`id` bigint not null auto_increment, `name` varchar
  (255), `pass` varchar(255), `pesel` varchar(255), `surname` varchar(255), primar
  y key (`id`))
  [ERROR] [localhost-startStop-1 06:06:19] (SchemaExport.java:create:387) unexpect
  ed token:  required: (
  </code></pre>
  
  <p>I don't know where the error could be, I'm also adding piece of db_scheme:<br/></p>
  
  <pre><code>CREATE TABLE Address (
  id INTEGER GENERATED BY DEFAULT AS IDENTITY primary key,
  city varchar(64),
  street varchar(64)
  );
  </code></pre>
  
  <p>In hsqldb gui there are no errors, when I'm deleting/adding scheme and data.</p>