4

由于JDK8 Oracle宣布不再支持,我需要将当前升级JDKJDK10.

经过研究,当前hibernate也需要从升级hibernate 4hibernate 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>
4

1 回答 1

11

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

您可以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>

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

记好

Hibernate 5.2.x 中不再存在hibernate-entitymanager-...jar原始代码片段使用的那个。pom.xml与 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. 你设置:<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

  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

     <?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">

补充说明

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

希望能帮助到你。

于 2018-06-07T18:51:29.157 回答