Enterprise Java

Access the EntityManager from Spring Data JPA

Spring Data JPA allows you to rapidly develop your data access layer through the use of Repository interfaces. Occasionally you will need to access the EntityManager from Spring Data JPA. This post shows you how to access the EntityManager.


The purpose of the EntityManager is to interact with the persistence context. The persistence context will then manage entity instances and their associated lifecycle. This was covered in my blog post on the JPA Entity Lifecycle

Spring Data JPA does an excellent job abstracting you from the EntityManager through its Repository interfaces –

But occasionally you need to access the EntityManager.


An example of this is the refresh method. The refresh method refreshes the state of an instance from the database, and overwrites the copy held by the EntityManager. This ensures the EntityManager manager has the most up to date version of the data

Spring Data JPA Example

Lets use the JPA object from my normal test ground

@Table(name = "PARKRUNCOURSE")
public class ParkrunCourse {
   @Column(name = "PRCOURSE_ID")
   private Long courseId;
   @Column(name = "COURSENAME")
   private String courseName;
   @Column(name = "URL")
   private String url;
   @Column(name = "AVERAGETIME")
   private Long averageTime;

And its associated repository –

public interface ParkrunCourseRepository extends CrudRepository {}

This is a standard implementation of a Spring repository, with the CrudRepository taking ParkrunCourse, and its key type Long

Create Custom Interfaces and Implmentation

The first step is to define a new interface with the same signature as the underlying EntityManager method we want to access –

public interface ParkrunCourseRepositoryCustom {
   void refresh(ParkrunCourse parkrunCourse);

The key point is the custom implementation must end with “Custom”, unless overridden in Spring Data configuration.

Next we provide the implementation for this interface, and inject the EntityManager –

import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import com.glenware.springboot.form.ParkrunCourse;
import org.springframework.transaction.annotation.Transactional;
public class ParkrunCourseRepositoryImpl implements ParkrunCourseRepositoryCustom {
   private EntityManager em;
   public void refresh(ParkrunCourse parkrunCourse) {

We must end our implementation name with “Impl”

We then change the ParkrunCourseRepository interface to –

public interface ParkrunCourseRepository extends CrudRepository, ParkrunCourseRepositoryCustom {

We can then refresh our JPA object –

private ParkrunCourseRepository parkrunCourseRepository;
ParkrunCourse parkrunCourse = parkrunCourseRepository.findOne(1L);
// Do some work & in the mean time the database has been updated by a batch job
// refresh object and now up to date


This approach shows how to access the EntityManager using Spring Data JPA. The advantage of this approach is you can access the EntityManager for a specific JPA implementation. The disadvantage of this approach is that you would need to repeat this task for each JPA implementation. The next post looks at a more generic approach to the custom repository implementation, allowing other JPA objects to benefit.

Published on Java Code Geeks with permission by Martin Farrell, partner at our JCG program. See the original article here: Access the EntityManager from Spring Data JPA

Opinions expressed by Java Code Geeks contributors are their own.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

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 ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


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.
Notify of

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

Inline Feedbacks
View all comments
Back to top button