1
votes

Error while creating Hibernate SessionFactory

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext

My POM file

============

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

This is how SessionFactory Created

@Bean
  public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource());
    sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
    hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
    hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
    sessionFactoryBean.setHibernateProperties(hibernateProperties);

    return sessionFactoryBean;
  }
1
Verify that you have the right versions of hibernate on your classpath and that you don't have conflicting versions, which can happen when various spring modules depend or introduce differing transitive dependencies.Naros
Your pom seems to be fine, i think you are missing transactionManager configuration try to add HibernateTransactionManagerVipul Panth
What is the package of LocalSessionFactoryBean? And show us the TransactionManager creation method.. with importsMaciej Kowalski
It was issue with the conflict between Spring & Hibernate versions. I was using org.springframework.orm.hibernate4.*; , now I changed the version to org.springframework.orm.hibernate5.*; . Issue is resolved . But is it not a bug in Spring boot where we are not giving any dependency version details in our pom.xml file.mbarish-me

1 Answers

2
votes

Solution is import org.springframework.orm.hibernate5.*;