【问题标题】:How to upgrade Hibernate from version 4.3 to 5.2 for migration to JDK 10?如何将 Hibernate 从版本 4.3 升级到 5.2 以迁移到 JDK 10?
【发布时间】:2018-10-16 20:18:22
【问题描述】:

由于JDK8 Oracle宣布不再支持,我需要将当前的JDK升级为JDK10

经过研究,目前的hibernate也需要从hibernate 4升级到hibernate 5,才能在JDK 10.运行

但是,有一些hibernate相关的库,我是否也应该升级,如果是,哪个版本适合?这是我当前pom.xml的摘录:

<properties>
<hibernate.version>4.3.11.Final</hibernate.version>
<java-version>1.7</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- hibernate -->
<dependency>
     <groupId>org.hibernate.javax.persistence</groupId>  
     <artifactId>hibernate-jpa-2.1-api</artifactId>
     <version>1.0.0.Final</version>
</dependency>

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>${hibernate.version}</version>
</dependency>

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-validator</artifactId>
     <version>5.1.2.Final</version>
</dependency>

【问题讨论】:

    标签: hibernate jpa hibernate-5.x java-10


    【解决方案1】:

    仅谈依赖关系,从 Hibernate 4.3.x 升级到 >= 5.2.x 非常简单。最新的 >= 5.2.x 版本非常可靠,并且已经被社区测试了很长一段时间。更新的版本 >= 5.3.x 已于 2020 年 11 月发布。

    您可以使用以下 sn-ps 在您的 pom.xml 中实现迁移:

    休眠 5.2.x

    <properties>
        <hibernate.version>5.2.18.Final</hibernate.version>
        <hibernate.validator.version>6.0.21.Final</hibernate.validator.version>
    
        <java-version>10</java-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <!-- hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    
    <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>
    

    休眠 5.3.x

    只需替换一个属性值即可:

    <hibernate.version>5.3.20.Final</hibernate.version>
    

    所有其他相关的传递依赖都是通过上述工件自动引入的。

    注意

    原来的pom.xml sn-p 使用的hibernate-entitymanager-...jar 在 Hibernate 5.2.x 中不再存在。与 JPA/EntityManager 相关的所有内容现在都包含在 hibernate-core-...jar 中。

    休眠验证器

    6.0.10版本开始,库全面支持JDK10:

    您现在可以通过 JDK 10 构建和使用 Hibernate Validator。

    参考见:http://in.relation.to/2018/05/15/hibernate-validator-6010-final-out/

    检查...

    此外,检查项目中的每个persistence.xml 文件,以便

    1. 你设置:&lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;

    2. 并将标头定义为符合 JPA 2.1

        <?xml version="1.0" encoding="UTF-8"?>
        <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
            http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
            version="2.1">
      

    3. 符合 JPA 2.2 as

         <?xml version="1.0" encoding="UTF-8" ?>
         <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">
    

    补充说明

    理论上,所有重要的依赖项都应该用上面的 sn-ps 引入到你的项目中。但是,在实践中,您将(很可能)在 compileruntime 与现有项目代码遇到一些重大更改。其中许多问题可以通过查看此处的官方迁移指南来解决:

    希望对你有帮助。

    【讨论】:

    猜你喜欢
    • 2018-11-27
    • 2018-07-02
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2011-05-29
    • 2021-09-25
    相关资源
    最近更新 更多