Home » Java » Enterprise Java » Add EntityManager.refresh to all Spring Data Repositories

About Martin Farrell

Martin Farrell
Martin is a Software Developer and Consultant specializing in Java and the Spring Framework. When not coding he spends time with his wife and two children, or cycling his bike.

Add EntityManager.refresh to all Spring Data Repositories

In my previous post Access the EntityManager from Spring Data JPA I showed how to extend a single Spring Data JPA repository to access the EntityManager.refresh method. This post demonstrates how to Add EntityManager.refresh to all Spring Data Repositories.

Source Code

The first step is to define your interface –

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);
}

The key points are –

  • @NoRepositoryBean – This prevents an instance of a Repository being created
  • Extending the CrudRepository – You need to decided which Repository to extend. Im using CrudRepository as this was used in the previous post
  • void refresh method signature is the same as the EntityManager.refresh method

Implementation

The next step is to implement this interface in a custom repository –

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);
    }
}

Key points are –

  • Extend SimpleJpaRepository repository. The SimpleJpaRepository is the default implementation for CrudRepository
  • Constructor is the SimpleJpaRepository constructor taking the JpaEntityInformation and EntityManager objects.
  • We save a local copy of the EntityManager
  • refresh method simply calls the EntityManager.refresh method

The final step is to let Spring Data know that its base class is 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);
   }
}

Key Points –

  • EnableJpaRepositories -This annotation enables JPA repositories, and will scan com.glenware.springboot by default
  • repositoryBaseClass attribute is used to let the Spring Data configuration know we are overriding the default base class

All Together Now

We can then use this repository in our classes so we change our repository from the previous post from CrudRepository to extend CustomRepository –

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

We can now access the EntityManager.refresh method using –

parkrunCourseRepository.refresh( parkrunCourse );

The above code was tested by running it against Spring Boot (1.5.6-Release) which used Spring Data JPA 1.11.6.Release. I can add the code to github if requested

Gotcha’s

One area you need to check is what version of Spring Data JPA you are running for extending repositories. I’ve had to adapt this approach for older repositories, although this is the current method using Spring Data JPA 1.11.6 Release

Published on Java Code Geeks with permission by Martin Farrell, partner at our JCG program. See the original article here: Add EntityManager.refresh to all Spring Data Repositories

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of