How do you create a custom repository class in Spring Data JPA?

A custom repository in Spring Data JPA is created by defining a repository interface that extends JpaRepository (or CrudRepository) with your entity and ID types, optionally adding your own derived or @Query-annotated methods beyond the built-in CRUD operations.

Key Points

Example

A ProductRepository needs both the standard CRUD methods and a custom method to find discounted products, so it extends JpaRepository and adds a derived method, findByDiscountGreaterThan(BigDecimal amount), without needing to write any SQL by hand.

public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findByDiscountGreaterThan(BigDecimal amount);

    @Query("SELECT p FROM Product p WHERE p.category = :category")
    List<Product> findByCategory(@Param("category") String category);
}

Interview Tip

A concise interview answer is

I extend JpaRepository with the entity and ID type to get all the standard CRUD, paging, and sorting behavior for free, then add my own methods either through Spring Data's derived-query naming or with @Query for anything more custom. For logic too complex to express declaratively, I'd add a separate custom interface and Impl class that Spring Data automatically wires in.

Last updated: 11 November 2026

Previous question: What are the main features of Spring Data JPA?

Next question: What is the difference between CrudRepository and JpaRepository?

More Spring Data JPA interview questions

All Spring Data JPA questions