Caused by: java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProper...

本文解决了一个关于SpringBoot结合SpringDataJPA时出现的与Hibernate版本冲突的问题。通过排除特定版本的Hibernate依赖,成功解决了启动时的错误。
  • 报错信息

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-31 07:24:56.020 ERROR 46256 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.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.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
     at edu.zhutao.springboot.springdatajpa.Application.main(Application.java:16) [classes/: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.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
     ... 16 common frames omitted

  • bug分析

bug环境:

SpringBoot1.5.6RELEASE

SpringDataJpa

对于我来说,在bug中我最关心的是CauseBy那一行,因为我觉得那一行可以提供丰富的信息,如上述黄色背景文字所述,往下看是发现这个错误的原因是在hibernate-entitymanager-5.2.10.jar文件中导致的,再继续往下看,发现spring-orm-4.3.10.RELEASE.jar,同属于ORM框架,而我之前又添加了hibernate-core的依赖,本身在SpirngBoot中集成SpringDataJpa是不需要添加这个jar包的,此时考虑jar包冲突,利用Maven的包结构查看jar包依赖关系


image

  • 解决方案:

去除spring-boot-starter-data-jpa的hibernate的低版本依赖,去除方式:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </exclusion>
    </exclusions>
</dependency>

重启,发现解决问题。

转载于:https://www.cnblogs.com/homeword/p/7456372.html

<think>嗯,用户遇到了java.lang.NoSuchMethodError,具体是关于java.time.LocalDateTime的问题。首先,我需要理解这个错误的原因。根据引用[4],NoSuchMethodError通常是因为类路径中存在版本不一致的jar包,或者编译时和运行时的类不同步。这里的问题出现在调用Cell.setCellValue(LocalDateTime)时,可能涉及Apache POI库的版本兼容性。 用户提到他们从POI 2.5升级到3.5后出现错误,而引用[2]和[5]指出升级POI版本可能导致方法不存在,比如HSSFDateUtil.isCellDateFormatted或Cell.getCellType()的变化。但当前的错误是关于LocalDateTime的setCellValue方法,这需要确认哪个POI版本开始支持该方法。 查阅POI的文档,发现LocalDateTime的支持可能在较新的版本中添加。例如,POI 4.0及以上版本可能引入了对Java 8时间类的支持。用户使用的POI 3.5可能还不支持这个方法,导致在运行时找不到setCellValue(LocalDateTime)方法,从而抛出NoSuchMethodError。 解决方案应包括检查依赖版本,确保所有相关库(如POI和其依赖的xmlbeans等)版本一致,并且升级到支持LocalDateTime的POI版本,如4.1.2或更高。同时,检查是否有其他库引入了旧版本的POI,导致冲突,可以使用Maven的依赖树分析,排除旧版本。 另外,用户可能需要调整代码,如果暂时无法升级POI,可以将LocalDateTime转换为Date类型使用旧方法,或者确认POI版本是否确实支持该方法的调用。此外,清理构建工具缓存,重新编译项目也是必要的步骤,避免旧版本残留。</think>### 原因分析 `java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Cell.setCellValue(Ljava/time/LocalDateTime;)V` 的根本原因是 **Apache POI库的版本与代码中调用的方法不兼容**。具体来说: 1. **方法缺失**:`Cell.setCellValue(LocalDateTime)` 方法仅在较新版本的POI中支持(如POI 4.1.2+),而当前项目中使用的POI版本可能较低(例如3.5或更早)[^4][^5]。 2. **依赖冲突**:项目可能同时引用了多个不同版本的POI库,导致运行时加载了旧版本,无法找到新方法[^1]。 --- ### 解决方案 #### 步骤1:检查并统一POI版本 **修改Maven/Gradle依赖**,确保使用支持`LocalDateTime`的POI版本(推荐4.1.2或更高): ```xml <!-- Maven示例 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` > 若使用Gradle,需同步更新`build.gradle`中的版本。 #### 步骤2:排除旧版本依赖 通过依赖树分析工具(如`mvn dependency:tree`)检查是否有其他库间接引用了旧版POI: ```xml <!-- 排除冲突示例 --> <dependency> <groupId>com.example</groupId> <artifactId>example-lib</artifactId> <exclusions> <exclusion> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> </exclusion> </exclusions> </dependency> ``` #### 步骤3:适配代码(可选) 若无法升级POI,可改用旧版API处理日期: ```java // 将LocalDateTime转换为java.util.Date LocalDateTime dateTime = LocalDateTime.now(); Date utilDate = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant()); cell.setCellValue(utilDate); // 旧版POI支持此方法 ``` #### 步骤4:清理并重新构建 清除构建缓存(如Maven的`target`目录、Gradle的`build`目录),确保依赖更新生效。 --- ### 验证方法 1. 运行`mvn dependency:tree | grep poi`(或Gradle对应命令),确认所有POI依赖版本一致。 2. 在IDE中查看`Cell`类的源码,验证是否存在`setCellValue(LocalDateTime)`方法。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值