title: Spring CrudRepository throws org.hibernate.LazyInitializationException
body: <p>i got a problem by using CrudRepository. Example: i have two entities, entity A has a collection of entity B.</p>
  
  <pre><code>class A {
    int id;
    int name;
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    Set&lt;B&gt; bs;
    // getters and setters
  }
  class B {
    int id;
    int name;
    @ManyToOne(mappedBy="bs")
    A a;
    // getters and setters
  }
  </code></pre>
  
  <p>then i got 2 repositories.</p>
  
  <pre><code>class ARepository extends CrudRepository&lt;A, int&gt;{}
  class BRepository extends CrudRepository&lt;B, int&gt;{}
  </code></pre>
  
  <p>but when i got this, i got a org.hibernate.LazyInitializationException, how can i avoid this?</p>
  
  <pre><code>@Service
  @Transactional(readOnly=true)
  class ServiceImpl implements Service {
  @Resource ARepository ar;
  
  @Override
  A a = ar.findOne(int id);
  }
  </code></pre>
  
  <p>here is the applicationContext.xml:</p>
  
  <pre><code>&lt;jpa:repositories base-package="com.myproject.repository" /&gt;
  
  &lt;context:component-scan base-package="com.myproject.*" /&gt;
  &lt;context:annotation-config /&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" &gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="persistenceUnitName" value="keep-apm" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="database" value="POSTGRESQL"/&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="username" value="root" /&gt;
      &lt;property name="password" value="root" /&gt;
      &lt;property name="driverClassName" value="org.postgresql.Driver" /&gt;
      &lt;property name="url" value="jdbc:postgresql://127.0.0.1:5432/db" /&gt;
  &lt;/bean&gt;
  &lt;bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory"&gt;
  &lt;/bean&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  </code></pre>
  
  <p>here is the web.xml</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;openSessionInViewFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;singleSession&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;openSessionInViewFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;filter&gt;
      &lt;filter-name&gt;struts2&lt;/filter-name&gt;
      &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;struts2&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;com.myproject.util.LogLocator&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  </code></pre>
  
  <p>a.bs(the collection) would not be loaded, and always throw out a <strong>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: no session or session was closed</strong> </p>
  
  <p>Thank you in advance!!</p>