中小型企业查询网站,北京计算机培训机构,手机如何制作游戏软件,wordpress 判断手机版在我以前的文章《从Spring Data JPA访问EntityManager》中#xff0c;我展示了如何扩展单个Spring Data JPA存储库以访问EntityManager.refresh方法。 这篇文章演示了如何将EntityManager.refresh添加到所有Spring Data Repository。 源代码 第一步是定义您的界面- package … 在我以前的文章《从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 CustomRepositoryT, ID extends Serializable
extends CrudRepositoryT, 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 CustomRepositoryImplT, ID extends Serializable
extends SimpleJpaRepositoryT, ID implements CustomRepositoryT, ID {private final EntityManager entityManager;public CustomRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {super(entityInformation, entityManager);this.entityManager entityManager;}OverrideTransactionalpublic void refresh(T t) {entityManager.refresh(t);}
} 重点是– 扩展SimpleJpaRepository存储库。 SimpleJpaRepository是CrudRepository的默认实现 构造函数是使用JpaEntityInformation和EntityManager对象的SimpleJpaRepository构造函数。 我们保存EntityManager的本地副本 refresh方法只需调用EntityManager.refresh方法 最后一步是让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 CustomRepositoryParkrunCourse, Long {
} 现在我们可以使用以下命令访问EntityManager.refresh方法 parkrunCourseRepository.refresh( parkrunCourse ); 通过针对使用Spring Data JPA 1.11.6.Release的Spring Boot1.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