Spring启动+ hibernate搜索:entityManagerFactory依赖错误

时间:2017-10-19 16:56:42

标签: java spring hibernate spring-boot hibernate-search

我在Spring Boot应用程序启动期间遇到了这个问题。

结果如下:

  

WARN 8532 --- [main] o.h.s.a.i.LuceneEmbeddedAnalyzerStrategy:HSEARCH000075:未指定配置设置hibernate.search.lucene_version:使用LUCENE_CURRENT。   WARN 8532 --- [main] ationConfigEmbeddedWebApplicationContext:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:使用>创建bean时出错name' entityManagerFactory'在类路径资源中定义[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]:调用init方法失败;嵌套异常是java.lang.NoSuchMethodError:> org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava / util的/属性;   INFO 8532 --- [main] o.apache.catalina.core.StandardService:停止服务[Tomcat]   INFO 8532 --- [main] utoConfigurationReportLoggingInitializer:

     

启动ApplicationContext时出错。要显示自动配置报告,请使用' debug'重新运行您的应用程序。启用。   ERROR 8532 --- [main] o.s.boot.SpringApplication:应用程序启动失败

     

org.springframework.beans.factory.BeanCreationException:使用名称' entityManagerFactory'创建bean时出错。在类路径资源中定义[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]:调用> init方法失败;嵌套异常是java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava / util / Properties;           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)〜[spring-context-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)〜[spring-context-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)〜[spring-context-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)〜[spring-boot-1.5.4.RELEASE.jar!/:1.5.4.RELEASE]           在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)[spring-boot-1.5.4.RELEASE.jar!/:1.5.4.RELEASE]           在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)[spring-boot-1.5.4.RELEASE.jar!/:1.5.4.RELEASE]           在org.springframework.boot.SpringApplication.run(SpringApplication.java:303)[spring-boot-1.5.4.RELEASE.jar!/:1.5.4.RELEASE]           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)[spring-boot-1.5.4.RELEASE.jar!/:1.5.4.RELEASE]           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)[spring-boot-1.5.4.RELEASE.jar!/:1.5.4.RELEASE]           在com.closdeal.restService.RestServiceApplication.main(RestServiceApplication.java:20)[classes!/:na]           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)〜[na:1.8.0_144]           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_144]           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_144]           在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_144]           在org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)[closdeal-rest-service-1.0.2.jar:na]           在org.springframework.boot.loader.Launcher.launch(Launcher.java:87)[closdeal-rest-service-1.0.2.jar:na]           在org.springframework.boot.loader.Launcher.launch(Launcher.java:50)[closdeal-rest-service-1.0.2.jar:na]           在org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)[closdeal-rest-service-1.0.2.jar:na]   引起:java.lang.NoSuchMethodError:org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava / util / Properties;           在org.hibernate.jpa.internal.EntityManagerFactoryImpl。(EntityManagerFactoryImpl.java:124)〜[hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]           在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:890)~ [hibernate-entitymanager-5.0.12.Final.jar!/:5.0.12.Final]           在org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)〜[spring-orm-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)〜[spring-orm-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)〜[spring-orm-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)〜[spring-orm-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)〜[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]           ...省略了24个常见帧

这些是在build.gradle中声明的依赖项:

dependencies {
    //Spring
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.7.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest', version: '1.5.7.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.7.RELEASE'
    compile group: 'org.springframework.security', name: 'spring-security-data', version: '4.2.3.RELEASE'
    compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.1.1.RELEASE'
    compile group: 'org.springframework.data', name: 'spring-data-rest-hal-browser', version: '3.0.0.RELEASE'

    //Hibernate
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.11.Final'
    compile group: 'org.hibernate', name: 'hibernate-spatial', version: '5.2.11.Final'
    compile group: 'org.hibernate', name: 'hibernate-search-orm', version: '5.8.1.Final'
//    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'

    //JSON
    compile("org.codehaus.jackson:jackson-mapper-asl")

    //Bases de datos
    compile("com.h2database:h2")
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.7-dmr'

    //Utilerías varias
    compile group: 'commons-io', name: 'commons-io', version: '2.5'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
    compile group: 'org.apache.commons', name: 'commons-text', version: '1.1'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.10'
    compile group: 'commons-validator', name: 'commons-validator', version: '1.6'
    compile group: 'net.glxn', name: 'qrgen', version: '1.4'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'


    //Test
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0")
    testCompile("org.junit.platform:junit-platform-runner:1.0.0")

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

当我检查了EntityManagerFactory类的getProperties方法的签名(进入hibernate-jpa-2.1-api-1.0.0.Final.jar)是这样的:

public Map<String, Object> getProperties();

我不知道为什么希望此方法返回一个Properties对象而不是一个map。这个罐子很旧(从2013年起保持不变);并且似乎没有其他jar将EntityManagerFactory类声明到类路径中。我已经检查了与此类似的其他问题,并且它们都指向将此类复制到类路径中,但似乎这不是问题所在。

提前多多感谢。

- 更新:我已根据Guillaume的建议更改了gradle依赖项,添加了dependencyManagement部分并编辑了依赖项:

dependencyManagement {
    imports {
        mavenBom 'org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE'
    }
}

dependencies {
    //Spring
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-rest'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    compile group: 'org.springframework.security', name: 'spring-security-data'
    compile group: 'org.springframework.security.oauth', name: 'spring-security-oauth2'
    compile group: 'org.springframework.data', name: 'spring-data-rest-hal-browser'

    //Hibernate
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.1.10.Final'
    compile group: 'org.hibernate', name: 'hibernate-spatial', version: '5.1.10.Final'
    compile group: 'org.hibernate', name: 'hibernate-search', version: '5.6.3.Final'
    compile group: 'org.hibernate', name: 'hibernate-search-engine', version: '5.6.3.Final'
    compile group: 'org.hibernate', name: 'hibernate-search-orm', version: '5.6.3.Final'

    //JSON
    compile("org.codehaus.jackson:jackson-mapper-asl")

    //Bases de datos
    compile("com.h2database:h2")
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.7-dmr'

    //Utilerías varias
    compile group: 'commons-io', name: 'commons-io', version: '2.5'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
    compile group: 'org.apache.commons', name: 'commons-text', version: '1.1'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.10'
    compile group: 'commons-validator', name: 'commons-validator', version: '1.6'
    compile group: 'net.glxn', name: 'qrgen', version: '1.4'
    compile group: 'com.vividsolutions', name: 'jts', version: '1.13'


    //Test
    testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0")
    testCompile("org.junit.platform:junit-platform-runner:1.0.0")

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

似乎工作正常。我会检查并稍后再确认。

2 个答案:

答案 0 :(得分:2)

AFAICS Spring Boot仍然基于Hibernate ORM 5.0.x.请参阅https://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/1.5.7.RELEASE/spring-boot-dependencies-1.5.7.RELEASE.pom

这就解释了为什么会出现此错误。

它也可以与Hibernate ORM 5.1一起使用,但5.2会改变很多东西。

因此,您应该将ORM降级到最新的5.0.x或尝试使用5.1.x(风险自负),并将Hibernate Search降级到最新的5.6.x.

答案 1 :(得分:0)

Spring Boot 1.5适用于Hibernate ORM 5.2 / search 5.8,但您必须覆盖所有依赖项。我怀疑你只是在原始配置中覆盖了其中的一些,并且可能在你的类路径中有多个版本的Hibernate ORM。

我不知道如何使用Gradle,但是使用Maven,你必须覆盖hibernate.version属性。

以下是Maven的示例,如果有帮助的话:

https://github.com/hibernate/hibernate-demos/blob/master/hibernate-search/hsearch-elasticsearch-wikipedia/pom.xml#L28