Cora.Kn.Kwok
Cora.Kn.Kwok

Reputation: 71

How to upgrade Hibernate from version 4.3 to 5.2 for migration to JDK 10?

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

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

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>

Upvotes: 4

Views: 15614

Answers (1)

MWiesner
MWiesner

Reputation: 9043

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 November 2020.

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

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

Hibernate 5.3.x

Just replace one property value to read:

<hibernate.version>5.3.22.Final</hibernate.version>

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

Note well

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.

Hibernate Validator

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

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

For reference see: http://in.relation.to/2018/05/15/hibernate-validator-6010-final-out/

Checks...

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

  1. you set: <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

  2. and define the header to be JPA 2.1 compliant:

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

or 3. to be JPA 2.2 compliant 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">

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:

Hope it helps.

Upvotes: 11

Related Questions