A Guide to EclipseLink with Spring – 使用Spring的EclipseLink指南

最后修改: 2017年 12月 19日

1. Overview


By default, Spring Data uses Hibernate as the default JPA implementation provider.

默认情况下,Spring Data使用Hibernate作为默认的JPA实现提供者。

However, Hibernate is certainly not the only JPA implementation available to us.


In this article, we’ll go through steps necessary to set up EclipseLink as the implementation provider for Spring Data JPA.

在本文中,我们将通过必要的步骤来设置em>EclipseLink作为Spring Data JPA的实现提供者。

2. Maven Dependency


To use it in our Spring application, we just need to add the org.eclipse.persistence.jpa dependency in the pom.xml of our project:



By default, Spring Data comes with the Hibernate implementation.

默认情况下,Spring Data带有Hibernate的实现。

Since we want to use EclipseLink instead as the JPA provider, we don’t need it anymore.


Therefore we can remove it from our project by excluding its dependencies:



The next step is to tell the Spring Framework that we want to use EclipseLink as the JPA implementation.


3. Spring Configuration


JpaBaseConfiguration is an abstract class which defines beans for JPA in Spring Boot. To customize it, we have to implement some methods like createJpaVendorAdapter() or getVendorProperties().

JpaBaseConfiguration是一个抽象类,它定义了Spring Boot中JPA的bean。要定制它,我们必须实现一些方法,如createJpaVendorAdapter()getVendorProperties()

Spring provides a configuration implementation for Hibernate out of the box called HibernateJpaAutoConfiguration. However, for EclipseLink, we have to create a custom configuration.


First, we need to implement the createJpaVendorAdapter() method which specifies the JPA implementation to use.

首先,我们需要实现createJpaVendorAdapter() 方法,该方法指定了要使用的JPA实现。

Spring provides an implementation of the AbstractJpaVendorAdapter for EclipseLink called EclipseLinkJpaVendorAdapter that we’re going to use in our method:


public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration { 

    protected AbstractJpaVendorAdapter createJpaVendorAdapter() { 
        return new EclipseLinkJpaVendorAdapter(); 

Also, we have to define some vendor-specific properties which will be used by EclipseLink.


We can add these via the getVendorProperties() method:


protected Map<String, Object> getVendorProperties() {
    HashMap<String, Object> map = new HashMap<>();
    map.put(PersistenceUnitProperties.WEAVING, true);
    map.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables");
    return map;

The class org.eclipse.persistence.config.PersistenceUnitProperties contains properties which we can define for EclipseLink.


In this example, we’ve specified that we want to use weaving and re-create the database schema when the application runs.


And that’s it! This is the whole implementation necessary to change from the default Hibernate JPA provider to EclipseLink.

就这样!这就是将默认的Hibernate JPA提供者改为EclipseLink所需的整个实现。

Note that Spring Data uses the JPA API and not any vendor specific methods. So, in theory, there should be no problem when switching from one vendor to another.

请注意,Spring Data使用JPA API,而不是任何供应商的特定方法。因此,从理论上讲,从一个厂商切换到另一个厂商时应该没有问题。

4. Conclusion


In this quick tutorial, we covered how to change the default JPA implementation provider used by Spring Data.

在这个快速教程中,我们介绍了如何改变Spring Data使用的默认JPA实现提供者。

We saw how quick and simple it is to change from Hibernate which is the default to EclipseLink.


As always, the full implementation of the examples is available over on Github.