在我以前的文章《从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);
}
}
重点是–
- 扩展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 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
本文介绍如何将EntityManager.refresh方法添加到所有Spring Data Repository,通过自定义接口和实现,利用SimpleJpaRepository来调用刷新操作。示例代码在Spring Boot 1.5.6 Release和Spring Data JPA 1.11.6.Release环境下测试有效,并提到了在不同版本Spring Data JPA中的注意事项。
755

被折叠的 条评论
为什么被折叠?



