7

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.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 2
    Incompatible Version of JPA and Hibernate – shazin Jan 09 '17 at 15:09
  • @shazin how to find compatible pair? – Dims Jan 09 '17 at 15:54
  • It looks like hibernate 5.2.x is supported by the 1.10.x line of spring-data-jpa based on the pom file for spring-data-jpa: https://github.com/spring-projects/spring-data-jpa/blob/1.10.x/pom.xml – LucasP Jan 12 '17 at 14:24
  • @LucasP then why it doesn't work? – Dims Jan 12 '17 at 14:59
  • Do you deploy your project in an application server, which maybe also provides hibernate libs with a different version? Or do you have other version conflicts? Can you post the output of `mvn dependency:tree`? – dunni Jan 13 '17 at 11:58
  • @dunni no I don't deploy to any server, it is standalone app. See SSCCE here https://github.com/dims12/TrySpringJpaPlusHibernate – Dims Jan 13 '17 at 12:59
  • Duplicate of [this question](http://stackoverflow.com/questions/37651837/integrate-hibernate-5-2-with-spring-framework-4-x?noredirect=1&lq=1). Make sure to use Spring 4.3+ if you want to use Hibernate 5.2. – manish Jan 15 '17 at 05:04
  • @manish how can I use Spring other than required by `spring-data-jpa`? – Dims Jan 15 '17 at 20:16

5 Answers5

7

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.

manish
  • 19,695
  • 5
  • 67
  • 91
0

usually when you have this Exception it means you should had hibernate-entitymanager to your porm. if you are using maven you can do it by adding this to your pom

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

I also use to define my dataSource through a bean I do not inject it with autowired as you did I don't know if it works that way. I rather do like this

 @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;
    }
Vivien SA'A
  • 727
  • 8
  • 16
0

I have just tested this versions in my project:

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

and it worked for me.

my config file is like this:

@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();
}



}
Irakli
  • 973
  • 3
  • 19
  • 45
  • Have you tried to perform any operation with database? – Dims Jan 13 '17 at 12:24
  • I changed version into my current project - where first step is authorization - then listing clients , client activities .... It was performing everything correctly - getting data from DB and writing updates back to SQL server. – Irakli Jan 13 '17 at 12:42
  • I don't see any database manipulation code in your example. See my example: https://github.com/dims12/TrySpringJpaPlusHibernate – Dims Jan 13 '17 at 12:55
  • I'll try it now. At first glance only differance is that I use maven you use grandle. I'm downloading grandle and will try your code now – Irakli Jan 13 '17 at 13:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133085/discussion-between-irakli-and-dims). – Irakli Jan 13 '17 at 13:45
0

Ok, the problem is a compatibility issue between Spring Data JPA and Hibernate. There seems to lack a synchronous explanation or @least version compatibility explanation between the guys @Spring ad the forks @Hibernate. For me, I have managed to make it work with the following versions

Spring Data JPA and Spring v5.0.0
1.10.0.RELEASE

and Hibernate 5.4.12.Final

Sometimes the issue arise when u forget to set the setPackagesToScan() property with your desired packages to be scanned in the LocalContainerEntityManagerFactorybean but this is not the case. So yeah check out yr Spring Data and Hibernate versions! Cheers

Jose Mhlanga
  • 805
  • 12
  • 14
-1

Do you need to include hibernate at all? Spring data-jpa includes all of the necessary hibernate components as far as I can see so I'm not sure why you need to add any extra hibernate dependencies

David
  • 589
  • 6
  • 21
  • Without Hibernate I don't have any `javax.persistence` classes. – Dims Jan 13 '17 at 12:57
  • 1
    Spring Data JPA is not a JPA provider. It is a library/framework that adds an extra layer of abstraction on the top of a JPA provider (like Hibernate). So you also need a JPA implementation. Reference : [Dzone](https://dzone.com/articles/what-is-the-difference-between-hibernate-and-sprin-1). – Pierre C Nov 02 '21 at 23:44