DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Projections/DTOs in Spring Data R2DBC
  • Less Code With Spring Data Rest
  • Calling Stored Procedures With IN and OUT Parameters From Spring Data JPA

Trending

  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  1. DZone
  2. Data Engineering
  3. Databases
  4. Adding EntityManager.refresh to All Spring Data Repositories

Adding EntityManager.refresh to All Spring Data Repositories

Bring EntityManager.refresh to all of your Spring Data repositories with this quick, simple implementation while making sure you clear the hurdles.

By 
Martin Farrell user avatar
Martin Farrell
·
Sep. 25, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
38.8K Views

Join the DZone community and get the full member experience.

Join For Free

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 decide which repository to extend. I'm using CrudRepository, as this was used in the previous post.
  • The 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&gt;
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);
    }
}


The key points are:

  • Extend the SimpleJpaRepository repository. The SimpleJpaRepository is the default implementation for CrudRepository.
  • The constructor is the SimpleJpaRepository constructor, taking the JpaEntityInformation and EntityManager objects.
  • We save a local copy of the EntityManager.
  • The 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.
  • The 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.

Spring Framework Spring Data Repository (version control) Data (computing)

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Projections/DTOs in Spring Data R2DBC
  • Less Code With Spring Data Rest
  • Calling Stored Procedures With IN and OUT Parameters From Spring Data JPA

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!