Access EntityManager From Spring Data JPA In Spring Boot

Using Repository interfaces, one can develop applications with an underlying database rapidly in Spring Boot. The Spring Data JPA handles the abstraction of the persistence layer quite well. Many times you probably won’t need anything else beyond what it has to offer. A lot can already be done with the exposed methods for data access from the interfaces alone.

Of course, it goes without saying that sometimes more is needed. This is when the need to access the EntityManager becomes handy. A more customized implementation can then be had once this Java Persistence API is exposed.

For example, I might need a custom solution to build a SQL query that will update a record for only those fields that were passed in our API endpoint. Sometimes due to business requirements, we don’t want any of the other fields to be modified, and we also don’t want to do an additional SELECT query from the database before doing said UPDATE operation.

Implementing a this kind of approach of accessing the EntityManager can be done by doing the following.

Let’s start with a custom interface. Take note that the name of the interface should be named the same way as the Repository we want to extend and should end in the keyword – “Custom”. This can be overridden in the Spring Data configuration, but we’re not going to take that up in this article. Below is what it would look like.

package xyz.joseyamut.repository.interfaces;

import xyz.joseyamut.dao.entity.BookEntity;

public interface BookRepositoryCustom {
    void updateBook(BookEntity entity);

Then we need to define the implementation for the single method in the interface – updateBook(BookEntity entity); The name of the implementing class must end in “Impl”. It extends the interface that was created above. The EntityManager is then injected and annotated with the @PersistenceContext annotation so it can interact with the persistence context. The @Transactional annotation at the method level here is more useful for writes, so that queries are done as a single atomic transaction instead of separate ones. An entity may involve more than one table here.

package xyz.joseyamut.repository.interfaces;

import xyz.joseyamut.dao.entity.BookEntity;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

public class BookRepositoryCustomImpl implements BookRepositoryCustom {

    private EntityManager manager;

    public void updateBook(BookEntity entity) {

        // do some custom code logic with the EntityManager here

Then we update the Repository interface to include the custom class like so:

package xyz.joseyamut.repository;

import xyz.joseyamut.dao.entity.BookEntity;
import xyz.joseyamut.repository.interfaces.BookRepositoryCustom;
import xyz.joseyamut.repository.interfaces.BookRepositoryCustomImpl;

@EnableJpaRepositories(basePackageClasses = BookRepositoryCustomImpl.class)
public interface BookRepository extends JpaRepository<BookEntity, String>, BookRepositoryCustom {

    // something more here


The custom method updateBook can now be accessed directly from the defined repository class above.

// ... Other class definitions omitted

    private BookRepository repository;

    public void update(BookEntity entity) {

And there we have it. Perhaps in a later blog article I’ll write about the custom update query implementation I did not so long ago.

Similar Posts: