分类
Spring Persistence

在spring中使用EclipseLink

A Guide to EclipseLink with Spring

1. 概述

Spring Data使用了Hibernate做为默认的JPA实现。其实这只是Spring Data JPA的一种选择,但不是唯一选择。本文中,我们将介绍如何使用 EclipseLink 来替代Hibernate实现Spring Data JPA。

2. Maven依赖

在Spring应用中使用EclipseLink,需要在pom.xml中添加 org.eclipse.persistence.jpa依赖:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa</artifactId>
    <version>2.7.7</version>
</dependency>

前面我们刚刚讲过,Spring Data默认使用了Hibernate做为了JPA实现。所以如果我们想使用EclipseLink来替代Hibernate的话,则应该移除默认的Hibernate实现。该操作应该在pom.xml中进行:

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

移除掉默认的Hibernate以后,接下来我们来展示如何将EclipseLink配置为Spring Data的实现。

3. 配置EclipseLink实现

Spring Boot提供了一个抽象类JpaBaseConfiguration来定义JPA实现。在配置EclipseLink实现时,仅需要继承该抽象类并重写createJpaVendorAdapter()与getVendorProperties()即可:

@Configuration
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {

    protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
        super(dataSource, properties, jtaTransactionManager);
    }

    @Override
    protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
       
    }

    @Override
    protected Map<String, Object> getVendorProperties() {
       
    }
}

createJpaVendorAdapter()方法中,我们返回一个Eclipse适配器:

    @Override
    protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
        return new EclipseLinkJpaVendorAdapter();
    }

getVendorProperties()返回适用于EclipseLink的配置信息:

    @Override
    protected Map<String, Object> getVendorProperties() {
        HashMap<String, Object> map = new HashMap<>();
        map.put(PersistenceUnitProperties.WEAVING,
                InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static");
        map.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables");
        return map;
    }

上述代码中分别对WEAVING(与惰性加载有关)以及DDL(与数据库初始化有关)进行了配置。配置过程中使用了org.eclipse.persistence.config.PersistenceUnitProperties类包中的静态变量,该类中包含了EclipseLink所有的可配置项。

然后,然后就没有然后了。此时我们仍然像以前一样来使用Spring Data JPA,不一样的是当前实现已经由Hibernate切换成了EclipseLink。

4. 测试

接下来,我们新建Student实体StudentRepository数据仓库、在H2数据库的支持下,进行单元测试。

@SpringBootTest
class StudentRepositoryTest {

    @Autowired
    StudentRepository studentRepository;

    @Test
    void save() {
        Student student = new Student();
        student.setName(RandomString.make(4));
        this.studentRepository.save(student);
    }
}

5. 结论

本文阐述了如何将Spring Data的默认实现由Hibernate切换为EclipseLink。相对于课堂上说过的很多遍的“面向接口编程、依赖于抽象而不依赖于具体”,相信本文能给我们带来更深的感受。

看一百次不如亲自做一次,听别人讲一百次也不如自己看一次。如果你想了解更多的细节还可以参考完整的示例代码。