【问题标题】:java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Propertiesjava.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties
【发布时间】:2017-04-09 17:48:45
【问题描述】:
@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 构建文件是

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 核心还是 spi 库添加到我的 gradle 构建文件中,我仍然得到同样的错误。 SpringBoot 在堆栈跟踪中使用spring-orm-4.3.7.RELEASE,然后添加休眠核心版本 5.2.9.FINAL 将其升级到该版本。我已经查看了这里提到的几乎所有不同的问题,但没有一个涉及这个版本。我还通读了休眠当前版本文档,它说它应该可以工作,但它不适合我,所以不知道为什么。

INFO 26648 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found。我将 JpaTransactionManager 更改为使用注入的 entityManagerFactory 作为建议的答案之一,但即使在使用 gradle 和 intellij 重建项目后它仍然无法正常工作。我查看了 Hibernate 5.2.9.FINAL 文档,该方法 getProperties() 不再存在,而是继承自 entitiyManagerFactory。此外,大多数答案都是在 xml config 中完成的,而不是在 Java 中完成的,因为我已经到了那里,并且没有一个答案描述了如何在 java config 情况下将其更改为地图而不是使用属性。当我将休眠版本降到 5.1.x 时,该错误消息会消失,但会显示 Java 8 LocalDate 类型的错误消息,我发现这是因为该版本的休眠不支持 Java 的功能。

【问题讨论】:

标签: java hibernate jpa intellij-idea orm


【解决方案1】:

如果您在从 gradle 3.x 升级到 4.x 时遇到此错误,这与解决传递 Hibernate 依赖项的方式有关。我发现 hibernate-entitymanager 现在包含在我的依赖项中。

运行gradle dependencyInsight --dependency hibernate 以查看您的休眠依赖项。如果您的项目中有现已弃用的 hibernate-entitymanager,请将其排除。

在 gradle 4.x 中,可以这样做

configurations.all {
    exclude(group: 'org.hibernate', module: 'hibernate-entitymanager')
}

【讨论】:

    【解决方案2】:

    根据 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 项目,并且只是将版本更改为当前的 Spring 仪表版本。我相信您会发现这不是重复,希望通过所有必要的步骤来解决此问题。

    【讨论】:

      【解决方案3】:

      你有什么版本的 Gradle?

      我问,因为我在将 Gradle 从 3.5 升级到 4.3 后出现了同样的错误。相同的 Hibernate 版本(5.2.9),相同的 Spring Boot(1.5.8),但在 Gradle 3.5 中。 - 好的,4.3 失败。

      我已经测试过,最新版本对我来说可以正常工作 3.5.1。我已经测试了从 4.0 到 4.4-rc-6 的版本(目前最新)。

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

      【讨论】:

      • 您好!当时的 gradle 版本在我的问题中是 3.4.1。我已经将该项目更新为使用 4.4 gradle 版本并且一切正常。不过,这可能是一个迟到的回复:-)
      • 12 月 5 日是 4.3 gradle - 我将检查我的项目 4.4 版本:)
      • 我从 gradle 3.5 升级到 4.6 时也发生了同样的事情!不明白为什么?
      • @RavishBhagdev,我知道这已经晚了一年,但你还有这个问题吗? Gradle 现在已经发布了 5.3,所以如果你还没有,可以使用它,我们从那里开始工作。同样,Spring Boot 已进入第 2 版,您也尝试过吗?
      • 哇。你认为我们应该在某个地方提出这个错误吗?
      猜你喜欢
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      相关资源
      最近更新 更多