title: Spring JPA and LazyInitializationException
body: <p>I'm using spring-jpa. I have 2 tests.</p>
  
  <pre><code>@Test
  @Transactional
  public void testFindAll() {
      List&lt;Engine&gt; eList = engineService.findAll();
      Engine e = eList.get(0); //this engine id=3
      List&lt;Translation&gt; tList = e.getTranslations();
      for(Translation t : tList) {
          ...
      }
  }
  </code></pre>
  
  <p>This method fails with this exception:</p>
  
  <blockquote>
    <p>org.hibernate.LazyInitializationException: failed to lazily initialize
    a collection of role: xxx.Engine.translations, could not initialize
    proxy - no Session</p>
  </blockquote>
  
  <p>However, this method works just fine:</p>
  
  <pre><code>@Test
  @Transactional
  public void testFindOne() {
      Engine e = engineService.findOne(3);
      List&lt;Translation&gt; tList = e.getTranslations();
      for(Translation t : tList) {
          ...
      }
  }
  </code></pre>
  
  <p>Why translation list is successfully loaded in one case, but not in another?</p>
  
  <p>EDIT: service/repo code:</p>
  
  <pre><code>public interface EngineRepository extends JpaRepository&lt;Engine, Integer&gt;
  {   
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>@Service
  @Transactional
  public class EngineService
  {
      @Autowired
      private EngineRepository engineRepository;
  
      public List&lt;Engine&gt; findAll()
      {
          return engineRepository.findAll();
      }
  
      public Engine findOne(Integer engId)
      {
          return engineRepository.findOne(engId);
      }
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>public class Engine implements Serializable {
      ...
      @OneToMany
      @JoinColumn(name="ID", referencedColumnName="TRAN_ID",  insertable=false, updatable=false, nullable=true)
      @LazyCollection(LazyCollectionOption.EXTRA)
      private List&lt;Translation&gt; translations;
      ...
  }
  </code></pre>
  
  <p>Config:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = {"xxx.dao"})
  @ComponentScan(basePackages = {"xxx.dao", "xxx.service", "xxx.bean"})
  @PropertySource("classpath:application.properties")
  public class SpringDataConfig {
  
    @Autowired
    private Environment environment;
  
    @Bean
    public DataSource dataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setUrl(environment.getProperty("db.url"));
      dataSource.setDriverClassName(environment.getProperty("db.driverClass"));
      dataSource.setUsername(environment.getProperty("db.username"));
      dataSource.setPassword(environment.getProperty("db.password"));
      return dataSource;
    }
  
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
      HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
      hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
  
      Properties properties = new Properties();
  
  
      properties.put("hibernate.dialect", environment.getProperty("hibernate.dialect"));
      properties.put("hibernate.show_sql", environment.getProperty("hibernate.showSQL"));
      properties.put("hibernate.format_sql", environment.getProperty("hibernate.formatSQL"));
  
      LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
      entityManagerFactoryBean.setDataSource(dataSource());
      entityManagerFactoryBean.setPackagesToScan("xxx.model");
      entityManagerFactoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter);
      entityManagerFactoryBean.setJpaProperties(properties);
  
      return entityManagerFactoryBean;
    }
  
    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(entityManagerFactory);
  
      return transactionManager;
    }
  }
  </code></pre>