Custom Repository in Spring Data JPA

Thanh Tran
Jan 11 · 2 min read

Spring Data JPA already provides many solutions that allow us querying data easier such as Query Method, Query Method, or four repository interface(JpaRepository, PagingAndSortingRepository, CrudRepository, Repository). These features are powerful, which help me a lot when building an application with Spring Data JPA. But a lot of time we still need to custom query or method to meet expectations or requirements.

Custom Repository in Spring Data JPA
Custom Repository in Spring Data JPA

The class design diagram above indicates how we can create a custom repository in Spring Data JPA. We just need to let EmployeeRepository implement the custom interface and define the implementation for the customized repository. Following this design, we can easy to apply query-by-example, the query-method as well as other Spring Data JPA features.

You might wonder where we put the @Repository annotation. The answer is at EmployeeRepository interface. We just have to provide the implementation of the customizing repository, other parts let Spring Framework complete. This article will not cover how this feature works under the hook. I only want to tell you that Spring Framework applies AOP and Proxy to build this powerful feature.

Custom Repository Code Example

We define a simple entity called Employee

And see how should we define the EmployeeRepository interface.

Now we can see that EmployeeRepository extends both EmployeeCustomizedRepository and JpaRepository we set EmployeeRepository extends JpaRepository to get all the powerful features of the built-in repository of Spring Data JPA.

Besides that we let EmployeeRepository extends EmployeeCustomizedRepository to make a custom repository.


Take a look at the implementation of EmployeeCustomizedRepository:

To simplify, I apply entityManage to make the custom method but you can implement it anyways to meets your expectations.

Test our work

I used CommandLineRunner to test our works, after running this program all matched records should be printed in the console log after UserCustomizedRepositoryImpl → implement


Up to now, we learned how we can design class, interface to able make a custom repository with Spring Data JPA. Belong that we have a code example to practice the theory.

Source demo:

Give me motivation

To get new article update please follow our publication or follow us on social



Sign up for Programming Sharings Newsletter

By Programming Sharing

Get new articles update by signing up our newsletter Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Thanh Tran

Written by

Software Engineer at Terralogic. Blogger and Amateur Investor

Programming Sharing

The publication to share programming knowledge

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store