Using EclipseLink to replace Hibernate in Spring Data JPA Starter

By default, when you use Spring Data JPA Starter in your Spring project, Hibernate will be the default implementation used. However, in some cases, you may not want to use this default implementation. For example, I’m converting this open source openid-connect https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server from Spring MVC to Spring Boot https://github.com/huongdanjavacom/openid-connect-spring-boot, this opensource is using EclipseLink instead of Hibernate. In my case, how to replace EclipseLink with Hibernate? I will guide you to do that in this tutorial.

First, I will create a new Spring Boot project:

Using EclipseLink to replace Hibernate in Spring Data JPA Starter

with Spring Data JPA Starter and PostgreSQL dependency as follows:

Using EclipseLink to replace Hibernate in Spring Data JPA Starter

Configure the database information in the application.properties file:

and run this Spring Boot application, you will see the following log message:

Read carefully the above loglines, you will see how Spring Data JPA Starter uses Hibernate as the default implementation.

Now, to replace this Hibernate default implementation with EclipseLink, first, I will declare the EclipseLink dependency as follows:

Then exclude Hibernate dependency in Spring Data JPA Starter dependency as follows:

Spring Boot uses the HibernateJpaAutoConfiguration class to configure all the necessary information for the Hibernate default implementation. If you take a look at this HibernateJpaAutoConfiguration class, you will see, all the information related to Hibernate is configured by Spring Boot in the HibernateJpaConfiguration class:

This HibernateJpaConfiguration class extends from the JpaBaseConfiguration class. This is the base class so we can extend and configure JPA for each implementation you want. Hibernate is an implementation and EclipseLink is also an implementation of JPA.

We will create a new class to configure JPA for EclipseLink:

The two methods that you need to implement in the EclipseLinkJpaConfiguration class are createJpaVendorAdapter() and getVendorProperties(). The createJpaVendorAdapter() method will return the implementation provider for JPA. By default, Spring already supports Hibernate and EclipseLink:

Using EclipseLink to replace Hibernate in Spring Data JPA Starter

so you just need to return the object of the EclipseLinkJpaVendorAdapter class.

The getVendorProperties() method helps us define some implementation-specific configurations. For EclipseLink, you can use the PersistenceUnitProperties class to add the properties it supports. Examples are as follows:

Now, if you run this application again, you will see the following log message:

Our application is using EclipseLink already!

Add Comment