Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find compatible version pair of Hibernate + Spring-JPA? (Could not open JPA EntityManager for transaction)

SSCCE is here: https://github.com/dims12/TrySpringJpaPlusHibernate

I am trying to run Spring JPA without persistence.xml and have the following config:

@Configuration
@ComponentScan
@ImportResource("classpath:data_source.xml")
@EnableJpaRepositories("org.inthemoon.train.chinese.repositories")
public class BaseConfig {
   @Autowired
   private DataSource dataSource;

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
      LocalContainerEntityManagerFactoryBean ans =
         new LocalContainerEntityManagerFactoryBean();
      ans.setDataSource(dataSource);
      ans.setJpaVendorAdapter(jpaVendorAdapter());
      ans.setPackagesToScan("org.inthemoon.train.chinese.data");
      return ans;
   }

   @Bean
   public JpaVendorAdapter jpaVendorAdapter() {
      HibernateJpaVendorAdapter ans = new HibernateJpaVendorAdapter();
      ans.setShowSql(false);
      ans.setGenerateDdl(true);
      ans.setDatabase(Database.H2);
      return ans;
   }

   @Bean
   public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
      JpaTransactionManager ans = new JpaTransactionManager();
      ans.setEntityManagerFactory(emf);

      return ans;
   }

}

it cause the following exception

Exception in thread "main" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
...

P.S. Is there any way to configure IoC from the first attempt?

UPDATE

I am using following libs:

compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.5.Final'

compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.10.5.RELEASE'

UPDATE 2

I tried 8 different versions of hibernate-core to build with spring-jpa of 1.10.5.RELEASE.

Versions from 5.2.1 to 5.2.6 were causing the same exception

NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;

Versions 5.1.3 and 5.0.11 were causing

ClassNotFoundException: org.hibernate.ejb.HibernateEntityManagerFactory

And the only version was causing something more complex was 5.2.0. It was causing

SchemaManagementException: Attempt to resolve foreign key metadata from JDBC metadata failed to find column mappings for foreign key named [FKLOK22W31RKBMIIC2J96T9LTCN

The questions arose:

1) Does this mean that namely version 5.2.0 is compatible with 1.10.5?

2) How would I know this without experiment?

3) Is this normal to guess versions this way? Wasn't the purpose of dependency management tools to avoid such things? If spring-data-jpa:1.10.5 depends on hibernate of 5.2.0 then why doesn't this described in it's POM?

UPDATE 3

Out of the box example: https://github.com/dims12/TrySpringJpaPlusHibernate

It doesn't work.

like image 818
Dims Avatar asked Jan 09 '17 14:01

Dims


People also ask

What is the default JPA provider in spring boot?

Spring Boot configures Hibernate as the default JPA provider, so it's no longer necessary to define the entityManagerFactory bean unless we want to customize it. Spring Boot can also auto-configure the dataSource bean, depending on the database we're using.

Does hibernate use EntityManager?

Hibernate provides implementation of JPA interfaces EntityManagerFactory and EntityManager . EntityManagerFactory provides instances of EntityManager for connecting to same database. All the instances are configured to use the same setting as defined by the default implementation.

What is EntityManager in spring boot JPA?

EntityManager is part of the Java Persistence API. Chiefly, it implements the programming interfaces and lifecycle rules defined by the JPA 2.0 specification. Moreover, we can access the Persistence Context by using the APIs in EntityManager.

Can we use JPA and Hibernate together?

You cant actually use both of them in the same application. For backwards compatibility. We are expanding our application and want to start using Spring Data JPA, but still keep the old hibernate implementation. Its better you develop your new application as a separate microservice and use spring data jpa ..


1 Answers

Spring Data JPA v1.10.6 depends on Spring v4.2 (v4.2.9 to be precise) and Spring v4.2 does not support Hibernate v5.2. Support for Hibernate v5.2 was added only in Spring v4.3. Therefore, you must upgrade the Spring dependencies to v4.3.


Adding the following dependencies to the Gradle build file should work:

compile 'org.springframework:spring-beans:4.3.4.RELEASE'
compile 'org.springframework:spring-context:4.3.4.RELEASE'
compile 'org.springframework:spring-context-support:4.3.4.RELEASE'
compile 'org.springframework:spring-core:4.3.4.RELEASE'
compile 'org.springframework:spring-jdbc:4.3.4.RELEASE'
compile 'org.springframework:spring-orm:4.3.4.RELEASE'
compile 'org.springframework:spring-tx:4.3.4.RELEASE'

Your modified code available on Github. Run Gradle tests as gradle test to verify that everything works fine.

like image 51
manish Avatar answered Oct 05 '22 05:10

manish