javascript
将EntityManager.refresh添加到所有Spring数据存储库
在我以前的文章《從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); }關(guān)鍵點是–
- @NoRepositoryBean –這樣可以防止創(chuàng)建存儲庫的實例
- 擴展CrudRepository –您需要確定要擴展的Repository。 我正在使用CrudRepository,因為上一篇文章中使用過
- void refresh方法簽名與EntityManager.refresh方法相同
實作
下一步是在自定義存儲庫中實現(xiàn)此接口–
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@Transactionalpublic void refresh(T t) {entityManager.refresh(t);} }重點是–
- 擴展SimpleJpaRepository存儲庫。 SimpleJpaRepository是CrudRepository的默認(rèn)實現(xiàn)
- 構(gòu)造函數(shù)是使用JpaEntityInformation和EntityManager對象的SimpleJpaRepository構(gòu)造函數(shù)。
- 我們保存EntityManager的本地副本
- refresh方法只需調(diào)用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);} }關(guān)鍵點 -
- EnableJpaRepositories-此批注啟用JPA存儲庫,默認(rèn)情況下將掃描com.glenware.springboot
- repositoryBaseClass屬性用于讓Spring Data配置知道我們正在覆蓋默認(rèn)基類
現(xiàn)在都在一起了
然后,我們可以在我們的類中使用此存儲庫,因此我們將存儲庫從CrudRepository的前一篇文章中進行更改,以擴展CustomRepository –
package com.glenware.springboot.repository;import com.glenware.springboot.form.ParkrunCourse;public interface ParkrunCourseRepository extends CustomRepository<ParkrunCourse, Long> { }現(xiàn)在,我們可以使用以下命令訪問EntityManager.refresh方法:
parkrunCourseRepository.refresh( parkrunCourse );通過針對使用Spring Data JPA 1.11.6.Release的Spring Boot(1.5.6-Release)運行上述代碼,進行了測試。 如果需要,我可以將代碼添加到github
Gotcha的
您需要檢查的領(lǐng)域之一是您正在運行哪個版本的Spring Data JPA以擴展存儲庫。 盡管這是使用Spring Data JPA 1.11.6 Release的當(dāng)前方法,但我不得不針對較舊的存儲庫采用這種方法。
翻譯自: https://www.javacodegeeks.com/2017/10/add-entitymanager-refresh-spring-data-repositories.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的将EntityManager.refresh添加到所有Spring数据存储库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux io查看命令(linux i
- 下一篇: DDOS工具(ddos工具排行榜)