如何将Hibernate从版本4.3升级到5.2以迁移到JDK 10? [英] How to upgrade Hibernate from version 4.3 to 5.2 for migration to JDK 10?

查看:357
本文介绍了如何将Hibernate从版本4.3升级到5.2以迁移到JDK 10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since JDK8 Oracle announced that no longer support, I am required to upgrade the current JDK to JDK10.

研究后,还需要当前的hibernatehibernate 4升级到hibernate 5,以便在JDK 10.上运行

After study, the current hibernate is also required to upgrade from hibernate 4 to hibernate 5, in order to run at JDK 10.

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

However, there are some hibernate related libraries, should I also upgrade, if yes, which version that is suitable? Here is an extract of my current 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 4.3.x 升级到> = 5.2.x 的过程很简单.非常简单.最新的> = 5.2.x 版本非常可靠,并且已经社区进行了测试.现在已经有一段时间了.较新的版本> = 5.3.x 已于2018年5月发布.

Speaking merely about dependencies, the upgrade from Hibernate 4.3.x to >= 5.2.x is pretty straight forward. The latest >= 5.2.x version is pretty solid and has been tested by the community for quite some time now. The more recent versions >= 5.3.x have been released in May 2018.

您可以通过以下代码片段在pom.xml中实现迁移:

You can achieve the migration in your pom.xml with the following snippets:

<properties>
    <hibernate.version>5.2.17.Final</hibernate.version>
    <hibernate.validator.version>6.0.10.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 5.3.x

Just replace one property value to read:

<hibernate.version>5.3.1.Final</hibernate.version>

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

All other relevant transitive dependencies are pulled in via the above artifacts automatically.

请注意

Note well

原始的pom.xml代码段所使用的hibernate-entitymanager-...jar在Hibernate 5.2.x中不再存在.与JPA/EntityManager有关的所有内容现在都已包含在hibernate-core-...jar中.

The hibernate-entitymanager-...jar which was used by your original pom.xml snippet no longer exists in Hibernate 5.2.x. Everything which is related to JPA/EntityManager is now incorporated in hibernate-core-...jar.

从版本 6.0.10 开始,该库完全支持JDK10:

Starting with the release of version 6.0.10, the library fully supports JDK10:

您现在可以在JDK 10中构建和使用Hibernate Validator.

You can now build and use Hibernate Validator with JDK 10.

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

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

Moreover, review every persistence.xml file in the project so that

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

要符合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">

更多评论

从理论上讲,所有重要的依赖关系都应使用上述代码片段绘制到您的项目中.但是,实际上,您(很可能)会在现有项目代码的 compile runtime 上遇到一些重大更改.通过查看以下官方的迁移指南,可以解决许多问题:

Further remarks

In theory, all important dependencies should be drawn into your project with the above snippets. However, in practice, you will (most likely) encounter some breaking changes at compile or runtime with your existing project code. Many of these can be resolved by checking the official migration guides here:

  • 版本 4.3.x-> 5.0.x :

https://github.com/hibernate/hibernate -orm/blob/5.0/migration-guide.adoc 是针对您的情况的推荐读物(因为您从4.3.x版开始)

https://github.com/hibernate/hibernate-orm/blob/5.0/migration-guide.adoc is a MUCH recommend read in your case (since you start at version 4.3.x)

版本 5.0.x-> 5.1.x :

http://staging.hibernate.org/orm/documentation/5.1/迁移/ 可以跳过,除非您使用BLOB和Oracle的DBMS或使用Hibernate的模式导出工具

http://staging.hibernate.org/orm/documentation/5.1/migration/ can be skipped except you make use of BLOBs and Oracle's DBMS or use Hibernate's schema export tools

版本 5.1.x-> 5.2.x :

https://github.com/hibernate/hibernate -orm/wiki/迁移指南--- 5.2 包含许多有关重要更改的信息,例如PK生成,LimitHandler等

https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2 contains a lot of information on important changes, such as PK generation, LimitHandler and others

版本 5.2.x-> 5.3.x :

https://github.com/hibernate/hibernate -orm/wiki/Migration-Guide --- 5.3 ,它添加了对JPA 2.2的完全支持,并且在JDK8/9/10环境中应该可以正常工作.

https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.3 which adds full JPA 2.2 support and should work fine in JDK8/9/10 environments.

希望有帮助.

这篇关于如何将Hibernate从版本4.3升级到5.2以迁移到JDK 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆