使用 Spring 引导时的两个 Hibernate 版本

Two versions of Hibernate when use Spring boot

我在 build.gradle 文件中明确指定了最新的 Hibernate 版本。我发现在构建 运行 之后我的依赖库列表中出现了两个不同版本的 Hibernate jar 文件。 Springboot引入低版本连我都想用5.4.2.Final。我应该使用 Spring 引导引入的版本还是有办法用更新的版本覆盖它?

我做了一些研究,但没有找到我正在寻找的答案。

参见 Excluding transitive dependencies:您可以从 Spring 引导传递依赖项中排除 hibernate 库,以强制 Gradle 使用您的版本。 首先你需要分析哪个Springboot依赖依赖于hibernate-core(一般是"org.springframework.boot:spring-boot-starter-data-jpa"):为此你可以使用gradle dependencies任务来浏览依赖关系图。

然后您可以按照文档中的描述排除特定的传递依赖:

dependencies{

    implementation ("org.springframework.boot:spring-boot-starter-data-jpa") {
        exclude group: "org.hibernate", module: "hibernate-core"
    }
    implementation "org.hibernate:hibernate-core:5.4.2.Final"
    ...
}

请注意,升级 hibernate-core 版本可能会对 Spring Jpa-related 库的行为产生副作用(请确保您使用的 Spring 引导版本与这个新版本 hibernate)

编辑:还有其他方法可以定义传递依赖约束,请参考官方文档(例如:Customizing Dependency Resolution Behavior