title: Spring, Hibernate and Lazy initialize
body: <p>I read many post about this problem on SO and didn't find any solution for me.</p>
  
  <p>So what I have</p>
  
  <p>Error</p>
  
  <blockquote>
    <p>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ua.home.bla.entity.Question.answers, no session or session was closed</p>
  </blockquote>
  
  <p>Entity
   </p>
  
  <pre class="lang-java prettyprint-override"><code> @Entity
      @Table(name = "Question")
      public class Question implements Serializable{
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
  
      private String question;
  
      @OneToMany(mappedBy="question")
      private List&lt;Answer&gt; answers;
      .....
  </code></pre>
  
  <p>DAO</p>
  
   
  
  <pre class="lang-java prettyprint-override"><code>   @Repository
  @Transactional 
  public class QuestionDAOImpl implements QuestionDAO {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      @SuppressWarnings("unchecked")
  //  @Transactional
      public List&lt;Question&gt; getQuestion() {
          // TODO Auto-generated method stub
          return sessionFactory.getCurrentSession().createQuery("from Question").list();
      }
  }
  </code></pre>
  
  <p>Service
   </p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class QuestionServiceImpl implements QuestionService {
  
      @Autowired
      private QuestionDAO questionDAO;
  
  
      @Transactional
      public List&lt;Question&gt; getQuestion() {
          return questionDAO.getQuestion();
      }
  
  }
  </code></pre>
  
  <p>Controller
   </p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  public class QuestionController {
  
      @Autowired
      private QuestionService questionService;
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String home(Model model){
          List&lt;Question&gt; qu =questionService.getQuestion();
          System.out.println(qu);
          return "home";
      }
  }
  </code></pre>
  
  <p>I try use OpenSessionInViewFilter, with and without url-pattern
   </p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;filter&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt; 
  </code></pre>
  
  <p>I try use annotation EAGER, other error</p>
  
  <blockquote>
    <p>org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError</p>
  </blockquote>
  
  <p>I don't know how use this solution  in my code Hibernate.initialize</p>
  
  <p>in data.xml add SessionFactoryUtils, from some answer in SO</p>
  
   
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml  version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
      &lt;tx:annotation-driven  transaction-manager="transactionManager" /&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="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="classpath:messages" /&gt;
          &lt;property name="defaultEncoding" value="UTF-8" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" /&gt;
  
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driverClassName}" 
          p:url="${jdbc.databaseurl}"
          p:username="${jdbc.username}" 
          p:password="${jdbc.password}" /&gt;
  
  &lt;bean name="hibernateSession" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession"
    scope="prototype"&gt;
          &lt;constructor-arg index="0" ref="hibernateSessionFactory"/&gt;
          &lt;constructor-arg index="1" value="false"/&gt;
          &lt;aop:scoped-proxy/&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="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
                  &lt;!-- &lt;value&gt;classpath*:**/hibernate.cfg.xml&lt;/value&gt; --&gt;
  
          &lt;/property&gt;
          &lt;property name="configurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.charSet"&gt;UTF-8&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Any idea for solution of my problem? Maybe somewhere I something missed?</p>
  
  <p>hibernate.cfg.xml</p>
  
   
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;mapping class="ua.home.bla.entity.Answer" /&gt;
          &lt;mapping class="ua.home.bla.entity.Question" /&gt;
          &lt;mapping class="ua.home.bla.entity.Contact" /&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>web.xml</p>
  
   
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!--  --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;
              /WEB-INF/spring/root-context.xml
          &lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Container Spring --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;!--
      &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;    --&gt;
  
      &lt;!-- Base Servlet --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
      &lt;filter&gt;
          &lt;filter-name&gt;charsetFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;encoding&lt;/param-name&gt;
              &lt;param-value&gt;UTF-8&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;charsetFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  &lt;!-- 
      &lt;filter&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt; --&gt;
  
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>root-context.xml
  </p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml  version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
          http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"&gt;
  
  
  
      &lt;!--  (@Annotation-based configuration)--&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;!-- (@Component, @Service)  --&gt;
      &lt;context:component-scan base-package="ua.home.bla.dao" /&gt;
      &lt;context:component-scan base-package="ua.home.bla.service" /&gt;
  
      &lt;!-- (Data Access Resources) --&gt;
      &lt;import resource="data.xml" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>controllers.xml
  </p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;!-- Dir with conrollers--&gt;
  
      &lt;context:component-scan base-package="ua.home.bla.web" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>servlet-context.xml
  </p>
  
  <pre class="lang-java prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  
      &lt;!-- @Controller etc. --&gt;
      &lt;annotation-driven /&gt;
  
      &lt;!-- css, javascript in dir webapp/resources  --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- jsp /WEB-INF/views --&gt;
      &lt;beans:bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- controllers settings --&gt;
      &lt;beans:import resource="controllers.xml" /&gt;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>Thx!</p>
  
  <p>----UPDATE-----</p>
  
  <p>Question entity method toString
   </p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
      public String toString() {
          return "Question [id=" + id + ", question=" + question + ", answers="
                  + answers + "]";
      }
  </code></pre>
  
  <p>Answer entity method toString
  </p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  public String toString() {
      return "Answer [id=" + id + ", answer=" + answer + ", isCorrect="
              + isCorrect + ", questions=" + question + "]";
  }
  </code></pre>
  
  <p>if I delete </p>
  
  <blockquote>
    <p>", questions=" + question</p>
  </blockquote>
  
  <p>no error, but don't have information about question entity. Not full result.</p>
  
  <p>Answer entity
  </p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class Answer  implements Serializable {
  
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
  
      private String answer;
  
      private byte isCorrect;
  
      @ManyToOne
      @JoinColumn(name="QuestionID")
      private Question question;
  </code></pre>