2017-04-09 136 views
1
@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; 
    } 
} 

這是我的配置類和我的gradle這個build文件是java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/UTIL /屬性

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' 
} 

雖然我gradle這個屬性文件是

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 

這個問題,我想使用Java 8的LOCALDATE的在我的實體類,但是,這仍然沒有不拋出此錯誤消息的工作:

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 

無論我將jadira core還是spi庫添加到我的gradle構建文件中,我仍然會得到相同的錯誤。 SpringBoot在堆棧跟蹤中使用spring-orm-4.3.7.RELEASE,然後添加hibernate核心版本5.2.9.FINAL將其升級到該版本。我已經看過幾乎所有在這裏提到的不同的問題,但沒有處理這個版本。我也讀過hibernate當前版本的文檔,它說它應該可以工作,但它不適合我,所以不知道爲什麼。

INFO 26648 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found。我將JpaTransactionManager更改爲使用注入的entityManagerFactory作爲建議的答案之一,但即使在使用gradle和intellij重建項目之後仍然無法正常工作。我查看了Hibernate 5.2.9.FINAL文檔,並且該方法getProperties()不再存在,而是從entitiyManagerFactory繼承。另外,大多數答案都是在xml配置文件中完成的,而不是在Java中完成的,因爲我已經到了那裏,沒有任何答案描述如何在java配置情況下將其更改爲映射,而不是使用屬性。當我將hibiernates版本下載到5.1.x時,會出現該錯誤消息,但顯示了Java 8 LocalDate類型的錯誤消息,我發現這是因爲在該版本的hibernate中不支持Java的功能。

+0

的可能的複製[彈簧試驗使用JUnit 4和Spring數據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) –

+0

這有很多步驟來最終解決問題,因爲每個潛在的解決方案都提出了其他不同的問題。我已經給出了我遵循的步驟和鏈接來解決我遇到的其他問題,這些問題可能有助於某人。鏈接絕對讓我朝着正確的方向前進。 – Sammy65

回答

0

根據Neil給出的鏈接,我能夠找到一種方法來創建和使用setJpaPropertyMap()來設置屬性Map。然後,它給了我一個不同的錯誤與LoadTimeWeavers我用

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

解決。這就給了我

java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation. 
爲此我使用的這個鏈接 http://gradle.1045684.n5.nabble.com/Java-Agent-LTW-problem-with-Gradle-and-Jetty-td4938600.html因爲它是一個項目的gradle解決和剛換了版本升級到當前春季儀器版本

的錯誤消息。我相信你會發現這不是希望通過所有必要的步驟來解決這個問題的重複。

1

你有什麼版本的Gradle?

我問,因爲我有升級Gradle從3.5到4.3後發生的相同的錯誤。相同的Hibernate版本(5.2.9),相同的Spring Boot(1.5.8),但在Gradle 3.5中。 - OK,4.3失敗。

我測試過的最新版本可以正常工作3.5.1。我測試過的版本從4.0到4.4-rc-6(最新在這一刻)。

distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-all.zip 
+0

你好!當時的gradle版本在我的問題中是3.4.1。我已經更新了該項目以使用4.4 gradle版本,並且一切正常。這可能是一個遲到的答覆,但:-) – Sammy65

+0

5月12日是4.3 gradle - 我會檢查我的項目4.4版本:) –