How to find a pair of compatible versions of Hibernate + Spring -JPA? (Failed to open JPA EntityManager for transaction)

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

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

@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 raises 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;
...

PS Is there a way to configure IoCon the first try?

UPDATE

I use the 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 hibernate-corebuild versions with spring-jpa from 1.10.5.RELEASE.

Versions from 5.2.1to 5.2.6cause the same exception

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

Versions 5.1.3and 5.0.11called

ClassNotFoundException: org.hibernate.ejb.HibernateEntityManagerFactory

And the only version that evoked something more complex: 5.2.0. It caused

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

There are questions:

1) , 5.2.0 1.10.5?

2) ?

3) ? ? spring-data-jpa:1.10.5 5.2.0, POM?

3

: https://github.com/dims12/TrySpringJpaPlusHibernate

.

+4
4

Spring JPA v1.10.6 Spring v4.2 (, v4.2.9), Spring v4.2 Hibernate v5.2. Hibernate v5.2 ​​ Spring v4.3. Spring 4.3.


Gradle :

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'

Github. Gradle gradle test, , .

+5

, , , hibernate-entitymanager. maven, , pom

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>${hibernate.version}</version>
</dependency>

, bean. autwired, , .

 @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/gescable");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }
0

? Spring data-jpa , , , -

0

:

<hibernate.version>5.2.5.Final</hibernate.version>
<version>1.10.5.RELEASE</version>

.

:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "ge.shemo.repositories")
@EnableJpaAuditing
public class PersistenceConfig {

@Autowired
private Environment env;

@Value("${init-db:false}")
private String initDatabase;


@Bean
public PlatformTransactionManager transactionManager() {
    EntityManagerFactory factory = entityManagerFactory().getObject();
    return new JpaTransactionManager(factory);
}


@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(Boolean.FALSE);
    vendorAdapter.setShowSql(Boolean.FALSE);

    factory.setDataSource(dataSource());
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("ge.shemo");

    Properties jpaProperties = getHibernateProperties();
    factory.setJpaProperties(jpaProperties);

    factory.afterPropertiesSet();
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
    return factory;
}

private Properties getHibernateProperties() {
    Properties prop = new Properties();
    /*prop.put("hibernate.format_sql", "true");*/
    /*prop.put("hibernate.hbm2ddl.auto", "update");*/
    //prop.put("hibernate.hbm2ddl.auto", "validate");
    // prop.put("hibernate.hbm2ddl.import_files","sql/import.sql");
    /*prop.put("hibernate.ejb.entitymanager_factory_name", "irakli");*/

    //prop.put("hibernate.show_sql", "true");
    prop.put("hibernate.dialect", "ge.shemo.config.SQLServerUnicodeDialect");
    return prop;
}


@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
    return new HibernateExceptionTranslator();
}


@Bean
public DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    ds.setUrl("jdbc:sqlserver://LinkToDB:1433;useUnicode=true;characterEncoding=UTF-8;DatabaseName=Your_DB");
    ds.setUsername("USER");
    ds.setPassword("PASS");
    return ds;
}




@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {

    DataSourceInitializer dataSourceInitializer = new     DataSourceInitializer();
    dataSourceInitializer.setDataSource(dataSource);
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    //databasePopulator.addScript(new ClassPathResource("db.sql"));
    dataSourceInitializer.setDatabasePopulator(databasePopulator);
    dataSourceInitializer.setEnabled(Boolean.parseBoolean(initDatabase));
    return dataSourceInitializer;
}


@Bean
public AuditorAware<Long> createAuditorProvider() {
    return new SecurityAuditor();
}

@Bean
public AuditingEntityListener createAuditingListener() {
    return new AuditingEntityListener();
}



}
0

Source: https://habr.com/ru/post/1666263/


All Articles