当前位置: 首页 > 面试题库 >

java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava / util / Properties

公冶子琪
2023-03-14
问题内容
@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 core还是spi库添加到gradle构建文件中,我仍然会遇到相同的错误。SpringBoot spring- orm-4.3.7.RELEASE按原样在stacktrace中使用,然后添加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
config中完成的,而不是在Java上完成的,因为我没有找到答案,没有一个答案描述了如何在java
config情况下将其更改为映射,而不是使用属性。当我将hibiernates版本降至5.1.x时,该错误消息消失了,但是显示的Java 8
LocalDate类型错误消息却是因为该版本的hibernate中不支持Java功能。


问题答案:

根据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
因为这是一个解决方案,只是更改了版本到当前的Spring工具版本。我相信您会发现这并不是解决所有问题所需的重复步骤。



 类似资料:
  • 问题内容: 以下是我的pom.xml依赖项 我也浏览了目录,但是它只包含一个目录。 我无法理解问题。 我正在使用Tomcat 8。 堆栈跟踪 问题答案: java.lang.NoSuchMethodError:javax.ws.rs.core.Application.getProperties()Ljava / util / Map; 该方法是在JAX-RS 2.0中引入的。当您混合使用JAX-R

  • 问题内容: 我正在尝试使用jersey 2.17 部署我的第一个rest应用程序。 我正在使用Maven,GlassFish 3.1.2.2进行部署。 应用程序在Eclipse(tomcat)中运行,但是在通过glassfish管理控制台进行部署时会出现以下错误。 pom.xml web.xml 问题答案: 好吧,看着你的问题。您正在使用的External Glassfish v 3.1.2.2与

  • 问题内容: 我在这里已经看到了许多与此异常相关的问题,并且大多数问题都通过添加来自Apache 的jar 得以解决。我尝试使用此解决方案,并为所有项目添加了相同版本的jar,但它似乎无法解决问题,我仍然面临问题。我知道已经问过这个问题,但是我想知道是否有人可以帮我解决这个具体问题。这是我的堆栈跟踪: 当我的方法调用抛出异常,从我的课用的,我一直在寻找信息和它说,使用的jar的版本必须为所有项目一样

  • 我正在尝试我的first REST应用程序。 以下是我的配置: Java 7 以下是Maven下载的罐子列表: 在服务器启动时,出现以下异常: 根据上面的日志,ApplicationHandler。init()方法正在调用应用程序。getProperties()。 ApplicationHandler位于jersey-server-2.22.2中。jar和应用程序在javax中。ws。rs-api

  • 我无法通过以下链接找到答案 一二三 以下是我的pom.xml依赖 我也看了一遍我的《代码》。m2/repository/javax/ws/rs/javax。ws。rs api目录,但它只包含一个目录。 我不能理解这个问题。 我正在使用Tomcat 8。 堆栈跟踪

  • 除了这个错误之外,我似乎在依赖项方面有很多问题,我正在试图修复它们,但我对Maven来说是一个完全的新手: 罐子未加载。参见Servlet规范2.3,第9.7.2节。违规类:javax/el/expression.class> 罐子未加载。参见Servlet规范2.3,第9.7.2节。违规类:javax/el/expression.class> null null null null null 我