0

I met 'org.hibernate.LazyInitializationException: could not initialize proxy - no Session' in spring data jpa.

Below is my code.

    // this is service layer
    @Transactional(rollbackFor = Exception.class)
    public BlogEntity saveBlog(BlogEntity blogEntityInput) {
        BlogEntity blogEntity = blogDao.find(blogEntityInput.getId());
        System.out.println(blogEntity);
        return blogDao.saveBlog(blogEntityInput);
    }

   // this is dao layer which will call
   @Override
   public BlogEntity find(Integer id) {
    return blogRepository.getOne(id); // JpaRepository#getOne
   }

The exception will occur when I print the blogEntity after I call getOne. I know that getOne is lazy loaded. But I have already add the @Transactional which will ensure the inner method will be in a hibernate session. How could the LazyInitializationException still occur?

Thanks.

Below is my full database related spring code.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "h2_entityManagerFactory",
  basePackages = { "com.taobao.rest.repository.h2" },
        transactionManagerRef = "h2_manager"

)
public class H2DBConfig {

  @Primary
  @Bean(name = "h2_dataSource")
  public DataSource dataSource(){
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("org.h2.Driver");
      dataSource.setUrl("jdbc:h2:~/new-taobao");
      dataSource.setUsername("taobao");
      dataSource.setPassword("");

      return dataSource;
  }

  @Primary
  @Bean(name = "h2_entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean h2EntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em
              = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] { "com.taobao.rest.entity.h2" });
      em.setPersistenceUnitName("h2_unit");
      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalProperties());

      return em;
  }

  Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty(
                "hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        properties.setProperty("hibernate.show_sql", "true");
        return properties;
    }


    @Bean(name = "h2_manager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("h2_entityManagerFactory") EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}
liam xu
  • 2,892
  • 10
  • 42
  • 65
  • 1
    Please add code of your BlogEntity entity. As I think in that entity there is reference of some other entity which when you call print method try to access that entity and it is not loaded with findOne method of BlogEntity . – Jaspreet Jolly Feb 20 '19 at 06:58
  • Can you paste the full service and comment the place, where the error occurs? – Andronicus Feb 20 '19 at 07:09
  • where are you calling `saveBlog` from? If it is from the same class the call won't go through the proxy doing the transaction handling and you will end up with a transaction that only spans the call to the repository. See this Q&A including comments: https://stackoverflow.com/questions/32156652/spring-transactional-annotation-is-not-working – Jens Schauder Feb 20 '19 at 07:28

0 Answers0