title: LazyLoading with spring data - collection is not associated with any session
body: <p>I have a small project with around 20 jpa entities; It all is about inserting and retrieving data.
  spring data was a nice solution for me but now I have a stopping problem with LazyLoading.</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app
      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"
      metadata-complete="true"
      version="2.5"&gt;
  
      &lt;description&gt;Multi vendor&lt;/description&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;webAppRootKey&lt;/param-name&gt;
          &lt;param-value&gt;webapp.root.springwebbasic&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/SpringApplicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;defaultHtmlEscape&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
  
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
  
  
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;springDispatcherServlet&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/SpringWebConfig.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;springDispatcherServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;session-config&gt;
          &lt;session-timeout&gt;60&lt;/session-timeout&gt;
      &lt;/session-config&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;jsp-config&gt;
          &lt;jsp-property-group&gt;
              &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
              &lt;page-encoding&gt;UTF-8&lt;/page-encoding&gt;
              &lt;trim-directive-whitespaces&gt;true&lt;/trim-directive-whitespaces&gt;
          &lt;/jsp-property-group&gt;
      &lt;/jsp-config&gt;
  
  &lt;filter&gt;
          &lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
          &lt;filter-class&gt;com.opensymphony.sitemesh.webapp.SiteMeshFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;sitemesh&lt;/filter-name&gt;
          &lt;url-pattern&gt;*.jsp&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
  
     &lt;filter&gt;    
          &lt;filter-name&gt;Spring OpenEntityManagerInViewFilter&lt;/filter-name&gt;    
          &lt;filter-class&gt;org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter&lt;/filter-class&gt;    
          &lt;init-param&gt;    
              &lt;param-name&gt;entityManagerFactoryBeanName&lt;/param-name&gt;    
              &lt;param-value&gt;entityManagerFactory&lt;/param-value&gt;    
          &lt;/init-param&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;init-param&gt;
          &lt;param-name&gt;flushMode&lt;/param-name&gt;
          &lt;param-value&gt;AUTO&lt;/param-value&gt;
      &lt;/init-param&gt;  
      &lt;/filter&gt;    
  
      &lt;filter-mapping&gt;    
          &lt;filter-name&gt;Spring OpenEntityManagerInViewFilter&lt;/filter-name&gt;    
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;    
      &lt;/filter-mapping&gt;   
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>springWebconfig.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
      &lt;context:component-scan base-package="com.mao" /&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" cache-period="3600" /&gt;
      &lt;mvc:default-servlet-handler /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>ApplicationContext.java which is java Configuration for the data access</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages="com.mao.services")
  public class ApplicationContext {
  
  
      private static final String PROPERTY_NAME_DATABASE_DRIVER = "com.mysql.jdbc.Driver";
      private static final String PROPERTY_NAME_DATABASE_PASSWORD = "ffs";
      private static final String PROPERTY_NAME_DATABASE_URL = " jdbc:mysql://localhost:3306/myfdfd";
      private static final String PROPERTY_NAME_DATABASE_USERNAME = "fdfd";
  
      private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "org.hibernate.dialect.MySQL5InnoDBDialect";
      private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "true";
      private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "org.hibernate.cfg.ImprovedNamingStrategy";
      private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "true";
      private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "com.mao.entites";
  
  
  
      @Bean
      public DataSource dataSource() {
          BoneCPDataSource dataSource = new BoneCPDataSource();
  
          dataSource.setDriverClass(PROPERTY_NAME_DATABASE_DRIVER);
          dataSource.setJdbcUrl(PROPERTY_NAME_DATABASE_URL);
          dataSource.setUsername(PROPERTY_NAME_DATABASE_USERNAME);
          dataSource.setPassword(PROPERTY_NAME_DATABASE_PASSWORD);
  
          return dataSource;
      }
  
      @Bean
      public JpaTransactionManager transactionManager() throws ClassNotFoundException {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
  
          transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
  
          return transactionManager;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
          LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  
          entityManagerFactoryBean.setDataSource(dataSource());
          entityManagerFactoryBean.setPackagesToScan(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN);
          entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
  
          Properties jpaProterties = new Properties();
  
          jpaProterties.put("hibernate.dialect", PROPERTY_NAME_HIBERNATE_DIALECT);
          jpaProterties.put("hibernate.format_sql", PROPERTY_NAME_HIBERNATE_FORMAT_SQL);
          jpaProterties.put("hibernate.ejb.naming_strategy", PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY);
          jpaProterties.put("hibernate.show_sql", PROPERTY_NAME_HIBERNATE_SHOW_SQL);
  
          entityManagerFactoryBean.setJpaProperties(jpaProterties);
          return entityManagerFactoryBean;
      }
  
  }
  </code></pre>
  
  <p>And I have repositories like this</p>
  
  <pre><code>public interface  VendorRepository  extends JpaRepository&lt;Vendor, Long&gt; {
  
  }
  </code></pre>
  
  <p>The problem appears in the controller when I try to fetch the lazy collections</p>
  
  <pre><code>@Controller
  @RequestMapping("/analysis")
  public class Analysis {
  @Resource
  private VendorRepository vendorRepository;
  @RequestMapping(value = "/vendors", method = RequestMethod.GET)
  @ResponseBody
  public String getVendors()
  {
      List&lt;Vendor&gt; vendorList= vendorRepository.findAll();
      Hibernate.initialize(vendorList.get(0).getVedorTechnologies()); 
      return vendorList.get(0).getVedorTechnologies().get(0).getName();
  }
  </code></pre>
  
  <p>entity class</p>
  
  <pre><code>@Entity
  @NamedQuery(name="Vendor.findAll", query="SELECT v FROM Vendor v")
  public class Vendor implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name="vendor_id")
      private int vendorId;
  
      @Column(name="vendor_name")
      private String vendorName;
  
      //bi-directional many-to-one association to VedorTechnology
      @OneToMany(mappedBy="vendor")
      private List&lt;VedorTechnology&gt; vedorTechnologies;
  
      public Vendor() {
      }
  }
  </code></pre>
  
  <p>I hope you find the problem!</p>
  
  <p><strong>update</strong></p>
  
  <p>when this line is called</p>
  
  <pre><code>Hibernate.initialize(vendorList.get(0).getVedorTechnologies()); 
  </code></pre>
  
  <p>I got this exception</p>
  
  <pre><code>HibernateException: collection is not associated with any session
  </code></pre>
  
  <p>I think the mean Idea that <code>OpenEntityManagerInViewFilter</code>is not working.</p>