You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Gradle+PostgreSQL环境下NoSuchMethodException异常排查

问题根源与解决方案

问题本质

这是PostgreSQL JDBC驱动版本与Hibernate版本不兼容导致的:

  • 你使用的Spring Boot Data JPA 3.0.2对应Hibernate 6.1.6.Final,该版本Hibernate需要PostgreSQL驱动中的PGInterval.getWholeSeconds()方法处理时间间隔类型
  • 但你指定的PostgreSQL驱动版本是42.1.4,这个老版本中不存在该方法,因此初始化entityManagerFactory时抛出找不到方法的异常

具体解决方案

方案1:让Spring Boot自动管理驱动版本(推荐)

直接移除PostgreSQL驱动的版本号,Spring Boot的依赖管理会自动匹配与当前JPA版本兼容的驱动版本:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.0.2'
    // 去掉版本号,由Spring Boot自动适配兼容版本
    implementation 'org.postgresql:postgresql'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

方案2:手动指定兼容的驱动版本

如果需要固定驱动版本,选择42.2.0及以上的版本(推荐42.5.x系列,是Spring Boot 3.0.x官方适配的稳定版本):

dependencies {
    // ... 其他依赖保持不变
    implementation 'org.postgresql:postgresql:42.5.1'
}

为什么删除重建能临时解决?

删除重建项目时,若未手动指定驱动版本,Spring Boot初始化向导会自动引入兼容的驱动版本,自然避开了版本冲突问题。

内容的提问来源于stack exchange,提问作者El Pandario

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:25:29