7
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

    @Bean(destroyMethod = "shutdown")
    public DataSource dataSource(){
        EmbeddedDatabaseBuilder databaseBuilder = new EmbeddedDatabaseBuilder();
        databaseBuilder.setType(EmbeddedDatabaseType.H2);
        databaseBuilder.addScript("classpath:db/migration/V1__Create_Books_Table.sql");
        databaseBuilder.addScript("classpath:db/migration/V2__Add_Books.sql");
        return databaseBuilder.build();
    }

    @Bean
    public JpaVendorAdapter vendorAdapter(){

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setDatabase(Database.H2);
        vendorAdapter.setShowSql(true);
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect");
        return vendorAdapter;
    }

    @Bean(name = "entityManagerFactory")
    public EntityManagerFactory managerFactory(){
    Properties jpaProperties = new Properties();
    jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");

    LocalContainerEntityManagerFactoryBean managerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    managerFactoryBean.setDataSource(dataSource());
    managerFactoryBean.setJpaVendorAdapter(vendorAdapter());
    managerFactoryBean.setPackagesToScan("com.sammy");
    managerFactoryBean.setJpaProperties(jpaProperties);
    managerFactoryBean.afterPropertiesSet();
    return managerFactoryBean.getObject();
}

    @Bean
    public PlatformTransactionManager transactionManager(){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(managerFactory());
        return transactionManager;
    }
}

That is my configuration class and my gradle build file is

buildscript {

    repositories {
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:${sonarVersion}"
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'org.sonarqube'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

jar{
    group 'com.sammy'
    version '1.0-SNAPSHOT'
}

dependencies {

    testCompile "junit:junit:${junitVersion}"
    testCompile "info.cukes:cucumber-java:${cucumberVersion}"
    testCompile "info.cukes:cucumber-junit:${cucumberVersion}"
    //testCompile "info.cukes:cucumber-spring:${cucumberVersion}"
    testCompile 'org.springframework.boot:spring-boot-starter-test'

    compile 'com.h2database:h2'
    compile "org.flywaydb:flyway-core:${flywayVersion}"
    compile "org.projectlombok:lombok:${lombokVersion}"
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }

    compile "org.hibernate:hibernate-core:${hibernateVersion}"
    compile 'org.springframework.boot:spring-boot-starter-aop'
    compile 'org.springframework.boot:spring-boot-starter-jetty'
    compile "io.springfox:springfox-swagger2:${swaggerVersion}"
    compile "org.jadira.usertype:usertype.core:${jadiraVersion}"
    compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.cloud:spring-cloud-starter-config'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    //compile 'org.springframework.boot:spring-boot-starter-data-mongodb'
}

task wrapper(type :Wrapper){
    gradleVersion = '3.4.1'
}

While my gradle properties file is

junitVersion = 4.12
sonarVersion = 2.2.1
flywayVersion = 4.1.2
swaggerVersion = 2.6.1
cucumberVersion = 1.2.5
lombokVersion = 1.16.14
jadiraVersion = 6.0.1.GA
hibernateVersion = 5.2.9.Final
springBootVersion = 1.5.2.RELEASE

This issue is, I want to use Java 8's LocalDate in my entity class but, this still doesn't work without throwing this error message:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/sammy/config/DataSourceConfig.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.sammy.SpringDataTutorials.main(SpringDataTutorials.java:18) [main/:na]
Caused by: java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
    at org.hibernate.jpa.internal.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:124) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:890) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 16 common frames omitted

Whether I add jadira core or spi library to my gradle build file, I still get the same error. SpringBoot uses spring-orm-4.3.7.RELEASE as is in the stacktrace then adding hibernate core version 5.2.9.FINAL upgrades it to that version. I've looked at almost all the different issues mentioned here but none deals with this version. I've also read through the hibernate current version document which says it should just work but it isn't for me so not sure why.

INFO 26648 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found. I changed the JpaTransactionManager to use the injected entityManagerFactory as one of those answers suggested but it still didn't work even after rebuilding the project with gradle and intellij. I looked at the Hibernate 5.2.9.FINAL docs and that method getProperties() no longer exists but inherited from entitiyManagerFactory. Also, most of the answers were done in xml config not the Java one as I've got up there and none of the answers described how to change it into a map in a java config situation rather than using the properties. When I drop hibiernates version to 5.1.x, that error message goes but the error message for the Java 8 LocalDate type is displayed which I found is because there's no support for Java's feature in that version of hibernate.

Sammy65
  • 627
  • 2
  • 12
  • 28
  • Possible duplicate of [Spring Test with JUnit 4 and Spring Data JPA: NoSuchMethodError org.hibernate.engine.spi.SessionFactoryImplementor.getProperties](http://stackoverflow.com/questions/39815784/spring-test-with-junit-4-and-spring-data-jpa-nosuchmethoderror-org-hibernate-en) – Neil Stockton Apr 09 '17 at 17:53
  • This had so many steps to finally fix the problem as each potential solution came up with different other issues. I've given the steps I followed and links to solve the other issues I encountered which can possibly help somebody. The link definitely set me in the right direction. – Sammy65 Apr 10 '17 at 14:05

3 Answers3

7

If you're getting this error when upgrading from gradle 3.x to 4.x, it has something to do with the way transitive Hibernate dependencies are resolved. I found that hibernate-entitymanager is now included in my dependencies.

Run gradle dependencyInsight --dependency hibernate to see your hibernate dependencies. If you have the now-deprecated hibernate-entitymanager in your project, exclude it.

In gradle 4.x, this can be done like

configurations.all {
    exclude(group: 'org.hibernate', module: 'hibernate-entitymanager')
}
Planky
  • 3,185
  • 3
  • 29
  • 39
4

What version of the Gradle do you have?

I ask, because I have the same error which occurs after upgrade Gradle from 3.5 to 4.3. The same Hibernate version (5.2.9), the same Spring Boot (1.5.8) but in Gradle 3.5. - OK, 4.3 fail.

I've tested and latest version working fine was for me 3.5.1. I've tested versions from 4.0 to 4.4-rc-6 (latest at this moment).

distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-all.zip
Krzysztof Szewczyk
  • 1,752
  • 1
  • 21
  • 25
  • Hello! The gradle version at that time is in my question as 3.4.1. I've updated that project to use 4.4 gradle version and all works well. This might be a late reply though :-) – Sammy65 Jan 01 '18 at 21:22
  • 5 Dec was 4.3 gradle - I will check in my project 4.4 version :) – Krzysztof Szewczyk Jan 04 '18 at 14:20
  • Same happened on me on upgrade from gradle 3.5 to 4.6! Don't understand why? – Ravish Bhagdev Mar 23 '18 at 12:15
  • @RavishBhagdev, I know this is a year late but do you still have this problem? Gradle now has 5.3 released so maybe use that if you already haven't and we work from there. Also by Spring Boot has moved into version 2, have you tried this as well? – Sammy65 Mar 27 '19 at 23:45
  • Wow. Do you think should we raise this bug somewhere? – Krzysztof Szewczyk Mar 29 '19 at 08:18
  • @Sammy65 I can't remember if/how I managed to solve the issue. It's been a year and a lot of code has flowed under the fingers ;-) Thanks for blast from the past anyway. – Ravish Bhagdev Mar 30 '19 at 02:48
1

Based on the link given by Neil, I was able to find a way to create and use the setJpaPropertyMap() to set the properties Map. Then it gave me a different error with LoadTimeWeaverswhich I used

@Bean
    public InstrumentationLoadTimeWeaver loadTimeWeaver()  throws Exception {
        InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
        return loadTimeWeaver;
    }

to solve. This then gave me the error message of

java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.

for which I used this link http://gradle.1045684.n5.nabble.com/Java-Agent-LTW-problem-with-Gradle-and-Jetty-td4938600.html since it's a gradle project to solve and just changed the version to the current Spring instrumentation version. I believe you'll see this isn't a duplication hopefully with all the required steps to solve this issue.

Sammy65
  • 627
  • 2
  • 12
  • 28