Social Media

Spring Data JPA – Custom Repository

Another useful feature of Spring Data is defining custom repositories. These involve defining two additional artifacts for your Spring data repository –

  • Custom Repository interface
  • Custom Repository implementation


1. Define custom repository interface –

public interface CustomExampleRepositoryCustom {
Integer customExampleMethod(String stringIn);

2. Define implementation class –

public class CustomExampleRepositoryCustomImpl implements CustomExampleRepositoryCustom {
private EntityManager em;
public Integer customExampleMethod(String stringIn) {
return new Integer(1);

3. Extend your Spring Data Repository –

public interface CustomExampleRepository extends CrudRepository<Documents, Long>, CustomExampleRepositoryCustom {

4. We can then inject and call the method as we normally would –

private CustomExampleRepository customExampleRepository;
Integer callCustomExampleMethodInteger = customExampleRepository.customExampleMethod("Hello World");

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: