1. Overview
1.概述
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.
然而,Hibernate肯定不是我们唯一可用的JPA实现。
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
2.Maven的依赖性
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:
要在我们的Spring应用程序中使用它,我们只需要在我们项目的pom.xml中添加org.eclipse.persistence.jpaa>依赖。
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.0</version>
</dependency>
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.
由于我们想用EclipseLink代替JPA提供者,我们不再需要它了。
Therefore we can remove it from our project by excluding its dependencies:
因此,我们可以通过排除其依赖关系将其从我们的项目中删除。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
The next step is to tell the Spring Framework that we want to use EclipseLink as the JPA implementation.
下一步是告诉Spring框架,我们要使用EclipseLink作为JPA的实现。
3. Spring Configuration
3.Spring配置
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.
Spring为Hibernate提供了一个开箱即用的配置实现,叫做HibernateJpaAutoConfiguration。然而,对于EclipseLink,我们必须创建一个自定义配置。
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:
Spring为EclipseLink提供了一个AbstractJpaVendorAdapter的实现,名为EclipseLinkJpaVendorAdapter,我们将在我们的方法中使用。
@Configuration
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
//...
}
Also, we have to define some vendor-specific properties which will be used by EclipseLink.
此外,我们还必须定义一些供应商特定的属性,这些属性将被EclipseLink使用。
We can add these via the getVendorProperties() method:
我们可以通过getVendorProperties()方法添加这些。
@Override
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.
类org.eclipse.persistence.config.PersistenceUnitProperties包含了我们可以为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
4.结论
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.
我们看到了从Hibernate(默认的)到EclipseLink.的改变是多么的快速和简单。
As always, the full implementation of the examples is available over on Github.
一如既往,这些示例的完整实现可在Github上获得。