将EntityManager.refresh添加到所有Spring数据存储库

本文介绍如何将EntityManager.refresh方法添加到所有Spring Data Repository,通过自定义接口和实现,利用SimpleJpaRepository来调用刷新操作。示例代码在Spring Boot 1.5.6 Release和Spring Data JPA 1.11.6.Release环境下测试有效,并提到了在不同版本Spring Data JPA中的注意事项。
摘要由CSDN通过智能技术生成

在我以前的文章《从Spring Data JPA访问EntityManager》中,我展示了如何扩展单个Spring Data JPA存储库以访问EntityManager.refresh方法。 这篇文章演示了如何将EntityManager.refresh添加到所有Spring Data Repository。

源代码

第一步是定义您的界面-

package com.glenware.springboot.repository;
 
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.CrudRepository;
 
import java.io.Serializable;
import java.util.Optional;
 
@NoRepositoryBean
public interface CustomRepository<T, ID extends Serializable>
extends CrudRepository<T, ID> {
    void refresh(T t);
}

关键点是–

  • @NoRepositoryBean –这样可以防止创建存储库的实例
  • 扩展CrudRepository –您需要确定要扩展的Repository。 我正在使用CrudRepository,因为上一篇文章中使用过
  • void refresh方法签名与EntityManager.refresh方法相同

实作

下一步是在自定义存储库中实现此接口–

package com.glenware.springboot.repository;
 
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
 
import javax.persistence.EntityManager;
import java.io.Serializable;
 
public class CustomRepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository>T, ID> implements CustomRepository<T, ID> {
 
    private final EntityManager entityManager;
 
    public CustomRepositoryImpl(JpaEntityInformation entityInformation, 
                                EntityManager entityManager) {
        super(entityInformation, entityManager);
        this.entityManager = entityManager;
    }
 
    @Override
    @Transactional
    public void refresh(T t) {
        entityManager.refresh(t);
    }
}

重点是–

最后一步是让Spring Data知道其基类是CustomRepositoryImpl –

package com.glenware.springboot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 
import com.glenware.springboot.repository.CustomRepositoryImpl;
 
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = CustomRepositoryImpl.class)
public class ParkrunpbApplication {
   public static void main(String[] args) {
      SpringApplication.run(ParkrunpbApplication.class, args);
   }
}

关键点 -

  • EnableJpaRepositories-此批注启用JPA存储库,默认情况下将扫描com.glenware.springboot
  • repositoryBaseClass属性用于让Spring Data配置知道我们正在覆盖默认基类

现在都在一起了

然后,我们可以在我们的类中使用此存储库,因此我们可以将存储库从CrudRepository的前一篇文章中更改为扩展CustomRepository –

package com.glenware.springboot.repository;
 
import com.glenware.springboot.form.ParkrunCourse;
 
public interface ParkrunCourseRepository 
   extends CustomRepository<ParkrunCourse, Long> {
}

现在,我们可以使用以下命令访问EntityManager.refresh方法:

parkrunCourseRepository.refresh( parkrunCourse );

通过针对使用Spring Data JPA 1.11.6.Release的Spring Boot(1.5.6-Release)运行上述代码,进行了测试。 如果需要,我可以将代码添加到github

Gotcha的

您需要检查的领域之一是您正在运行哪个版本的Spring Data JPA以扩展存储库。 尽管这是使用Spring Data JPA 1.11.6 Release的当前方法,但我不得不针对较旧的存储库采用这种方法。

翻译自: https://www.javacodegeeks.com/2017/10/add-entitymanager-refresh-spring-data-repositories.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值