title: Lazy Loading error
body: <p>I know that this issue was already raised here several time. However I couldn't find any solution which will help so I decided to  post it again.</p>
  
  <p>I'm using Hibernate with Spring MVC framework and I'm trying to fetch my class wth sub class:</p>
  
  <pre><code>public class Scene implements Serializable{
  private Long id;
  private Author author;
  //omitting getters and setters
  } 
  
  public class Author{
  private Long id;
  Private String  name; 
  //omitting getters and setters
  }
  </code></pre>
  
  <p>My Hibernate configuration is the following:</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
    &lt;property name="driverClassName" value="org.postgresql.Driver" /&gt;
    &lt;property name="url" value="jdbc:postgresql://127.0.0.1/doolloop2" /&gt;
    &lt;property name="username" value="doolloop2" /&gt;
    &lt;property name="password" value="doolloop" /&gt;
  &lt;/bean&gt;
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
    &lt;property name="dataSource" ref="dataSource" /&gt;
       &lt;property name="mappingLocations"&gt;
        &lt;list&gt;
          &lt;value&gt;WEB-INF/mapping/Scene.hbm.xml&lt;/value&gt;
          &lt;value&gt;WEB-INF/mapping/Author.hbm.xml&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.PostgreSQLDialect&lt;/prop&gt; 
    &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt; 
    &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt; 
    &lt;prop key="hibernate.cache.use_second_level_cache"&gt;true&lt;/prop&gt; 
    &lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt; 
    &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&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="txService" class="com.doolloop.services.TxDataBaseServiceImpl"&gt;
          &lt;property name="sessionFactory"&gt;
          &lt;ref local="sessionFactory"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt; 
  </code></pre>
  
  <p>Now I have methods which gets my scene:</p>
  
  <pre><code>@Override
      @Transactional(readOnly = true)
      public Scene getScene(Long id) {
          Session session = this.sessionFactory.getCurrentSession();
          Query q = session.createQuery("select scene from com.doolloop.objects.Scene scene where scene.id=:id");
          q.setLong("id", id);
          Scene scene = (Scene)q.uniqueResult()
          return scene;
  
      }
  </code></pre>
  
  <p>and my spring mvc code:</p>
  
  <pre><code> @RequestMapping(value="/getSceneMetaData.dlp",method = RequestMethod.GET)
       @ResponseBody
       public Scene getSceneMetaData(@RequestParam String id){
          Scene scene = service.getScene(Long.parseLong(id));
          return scene;
      }
  </code></pre>
  
  <p>When I'm executing it i got the followng exception:</p>
  
  <pre><code>Dec 5, 2010 12:29:21 PM org.hibernate.LazyInitializationException &lt;init&gt;
      SEVERE: failed to lazily initialize a collection of role: com.doolloop.objects.Scene.author, no session or session was closed
      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.doolloop.objects.Scene.vauthor, no session or session was closed
  </code></pre>
  
  <p>I googled it and understand that it happens because it tries to fetch author when Transaction is closed and that author class is lazy by default, but what I can't understand is how should I modify my code n order to solve it. </p>
  
  <p>Any suggestion s more the welcomed. </p>
  
  <p>UPD: here is the mapping files for both classes:</p>
  
  <pre><code>&lt;class
          name="com.doolloop.objects.Scene"
          table="scene"
      &gt;
          &lt;id
              name="id"
              column="Id"
              type="java.lang.Long"
              unsaved-value="null"
          &gt;
           &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;doolloop2.sceneseq&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
      &lt;many-to-one
           name="author"
           class="com.doolloop.objects.Author"
           cascade="all"
           column="authorid"
           unique="false"
       /&gt;  
      &lt;/class&gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class
          name="com.doolloop.objects.Author"
          table="authors"
      &gt;
          &lt;id
              name="id"
              column="Id"
              type="java.lang.Long"
              unsaved-value="null"
          &gt;
           &lt;generator class="sequence"&gt;
                  &lt;param name="sequence"&gt;doolloop2.authorseq&lt;/param&gt;
              &lt;/generator&gt;
          &lt;/id&gt;
          &lt;property
              name="name"
              update="true"
              insert="true"
              not-null="false"
              unique="false"
              type="java.lang.String"
          &gt;
              &lt;column name="name" /&gt;
          &lt;/property&gt;
               &lt;property
              name="email"
              update="true"
              insert="true"
              not-null="false"
              unique="false"
              type="java.lang.String"
          &gt;
              &lt;column name="email" /&gt;
          &lt;/property&gt;
          &lt;property
              name="site"
              update="true"
              insert="true"
              not-null="false"
              unique="false"
              type="java.lang.String"
          &gt;
              &lt;column name="site" /&gt;
          &lt;/property&gt;
         &lt;property
              name="contactInfo"
              update="true"
              insert="true"
              not-null="false"
              unique="false"
              type="java.lang.String"
          &gt;
              &lt;column name="contactInfo" /&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  </code></pre>
  
  <p><strong>UPD2</strong> I added the following to my web.xml:</p>
  
  <pre><code> &lt;listener&gt;
  &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;WEB-INF/applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
    &lt;filter&gt;
      &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/getSceneMetaData.dlp&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>it gave me the following exception:</p>
  
  <pre><code>org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]-&gt;com.doolloop.objects.Author_$$_javassist_4["name"])
  </code></pre>
  
  <p><strong>Update3:</strong></p>
  
  <p>This is the log4j output for this operation:</p>
  
  <pre><code>2010-12-06 11:48:09,031 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter
  2010-12-06 11:48:09,036 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'sessionFactory'
  2010-12-06 11:48:09,040 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Opening single Hibernate Session in OpenSessionInViewFilter
  2010-12-06 11:48:09,042 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
  2010-12-06 11:48:09,048 DEBUG [org.springframework.web.servlet.DispatcherServlet] - DispatcherServlet with name 'doolloop' processing GET request for [/dlp/getSceneMetaData.dlp]
  2010-12-06 11:48:09,056 DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapping [/getSceneMetaData.dlp] to HandlerExecutionChain with handler [com.doolloop.controllers.SceneController@21c33b55] and 2 interceptors
  2010-12-06 11:48:09,060 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Last-Modified value for [/dlp/getSceneMetaData.dlp] is: -1
  2010-12-06 11:48:09,142 DEBUG [org.springframework.core.convert.support.GenericConversionService] - Converting value '140' of [TypeDescriptor java.lang.String] to [TypeDescriptor @org.springframework.web.bind.annotation.RequestParam java.lang.String]
  2010-12-06 11:48:09,146 DEBUG [org.springframework.core.convert.support.GenericConversionService] - Converted to '140'
  2010-12-06 11:48:09,148 DEBUG [org.springframework.web.bind.annotation.support.HandlerMethodInvoker] - Invoking request handler method: public com.doolloop.objects.Scene com.doolloop.controllers.SceneController.getSceneMetaData(java.lang.String)
  2010-12-06 11:48:16,401 DEBUG [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver] - Resolving exception from handler [com.doolloop.controllers.SceneController@21c33b55]: org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]-&gt;com.doolloop.objects.Author_$$_javassist_4["name"])
  2010-12-06 11:48:16,561 DEBUG [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver] - Resolving exception from handler [com.doolloop.controllers.SceneController@21c33b55]: org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]-&gt;com.doolloop.objects.Author_$$_javassist_4["name"])
  2010-12-06 11:48:16,568 DEBUG [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] - Resolving exception from handler [com.doolloop.controllers.SceneController@21c33b55]: org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]-&gt;com.doolloop.objects.Author_$$_javassist_4["name"])
  2010-12-06 11:48:16,572 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Could not complete request
  org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: com.doolloop.objects.Scene["author"]-&gt;com.doolloop.objects.Author_$$_javassist_4["name"])
      at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:215)
  
  //// Entire stack trace goes here.
  
  2010-12-06 11:48:16,660 DEBUG [org.springframework.orm.hibernate3.support.OpenSessionInViewFilter] - Closing single Hibernate Session in OpenSessionInViewFilter
  2010-12-06 11:48:16,662 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
  </code></pre>
  
  <p><strong>Update4:</strong></p>
  
  <p>I see only one select n console:</p>
  
  <pre><code>Hibernate: select scene0_.Id as Id20_0_, scene0_.name as name20_0_, scene0_.description as descript3_20_0_, scene0_.defaultView as defaultV4_20_0_, scene0_.authorid as authorid20_0_ from scene scene0_ where scene0_.Id=?
  </code></pre>
  
  <p>However if I modify my getScene method that way:</p>
  
  <pre><code> public Scene getScene(Long id) {
          Session session = this.sessionFactory.getCurrentSession();
          Query q = session.createQuery("select scene from com.doolloop.objects.Scene scene where scene.id=:id");
          q.setLong("id", id);
          Scene scene = (Scene)q.uniqueResult()
          **Author author = scene.getAuthor();**
          return scene; 
  </code></pre>
  
  <p>I will see second select appears:</p>
  
  <pre><code>Hibernate: select author0_.Id as Id23_0_, author0_.name as name23_0_, author0_.email as email23_0_, author0_.site as site23_0_, author0_.contactInfo as contactI5_23_0_ from authors author0_ where author0_.Id=?
  </code></pre>
  
  <p>but author object wll be still empty.</p>
  
  <p>calling the same getter from controller causes exception.</p>