Quick revision
The short answer
Define a fragment interface such as interface OrderRepositoryCustom { List<Order> search(SearchCriteria c); }, implement it in a class named OrderRepositoryCustomImpl, the fragment interface name plus the Impl postfix, that injects the EntityManager and builds the query with JPQL or the Criteria API, and make the repository extend both: interface OrderRepository extends JpaRepository<Order, Long>, OrderRepositoryCustom. Spring Data finds the implementation by that name in the scanned packages and combines it into the repository proxy; the postfix can be changed with repositoryImplementationPostfix.
What to remember
In this answer, we saw that a custom repository method lives in a fragment: an interface such as OrderRepositoryCustom and a class named OrderRepositoryCustomImpl that injects the EntityManager and builds the query with Criteria or JPQL. The repository extends both JpaRepository and the fragment, and Spring Data finds the implementation by its name, interface plus Impl, in the scanned packages and combines it into the repository proxy, with repositoryImplementationPostfix available to change the postfix.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Define a fragment interface such as interface OrderRepositoryCustom { List<Order> search(SearchCriteria c); }, implement it in a class named OrderRepositoryCustomImpl, the fragment interface name plus the Impl postfix, that injects the EntityManager and builds the query with JPQL or the Criteria API, and make the repository extend both: interface OrderRepository extends JpaRepository<Order, Long>, OrderRepositoryCustom. Spring Data finds the implementation by that name in the scanned packages and combines it into the repository proxy; the postfix can be changed with repositoryImplementationPostfix.
Why this question is asked
Derived query methods and @Query cover most needs, but sooner or later a repository needs logic: a search whose conditions depend on several optional inputs, a query built from user-chosen columns, or a call that uses a Hibernate feature Spring Data does not expose. Custom repository fragments let us write that code ourselves while callers keep using a single repository interface. Interviewers ask for the full setup because the naming rule is easy to get wrong, and a wrong name produces a confusing startup error. In this answer, we will recap how a repository gets its implementation, write the fragment interface and its implementation, plug it into the repository, see how Spring Data finds it, and look at the postfix setting and common mistakes.
1A Quick Recap: Repositories Are Composed From Fragments
A Spring Data repository is an interface. At startup, Spring Data creates a proxy, an object generated at runtime that implements the interface. Each method call is routed to whatever can handle it: inherited CRUD methods go to SimpleJpaRepository, derived and @Query methods run their prepared queries.
A fragment is one more place a call can be routed to: an interface with methods we implement ourselves, plus a class that implements it. When the repository interface extends the fragment interface, the proxy routes those methods to our class. Callers see one repository; behind it are several pieces.
2The Fragment Interface
The fragment interface declares only the custom methods. It is a plain interface; it does not extend Repository, so Spring Data does not try to make it a repository of its own.
3The Implementation Class
The implementation's name matters: it must be the fragment interface's simple name plus the postfix Impl, so OrderRepositoryCustomImpl. It is a normal Spring bean, so it can inject the EntityManager and other beans. Here it uses the Criteria API so that each optional criterion adds a condition only when present.
Notice that the class needs no @Repository or @Component annotation: Spring Data detects it and registers it as a bean. Adding @Component does no harm. The same method could build a JPQL string instead, with em.createQuery("select o from Order o where ...", Order.class), appending conditions and binding parameters by name; Criteria is safer when conditions vary, because there is no string concatenation.
4Plug the Fragment Into the Repository
The repository interface extends both JpaRepository and the fragment interface. Callers inject OrderRepository and see the CRUD methods, any derived methods and search in one place.
The service has no idea which methods are generated and which are hand-written, which is exactly the point.
5How Spring Data Finds the Implementation
While building OrderRepository, Spring Data looks at each interface it extends. For every interface that is not a Spring Data base interface, it searches the packages scanned for repositories, by default the package of the application class and below, for a class named after that interface plus the postfix: OrderRepositoryCustom becomes OrderRepositoryCustomImpl. If it finds one, it registers it as a bean and adds it as a fragment of the proxy. Calls to search are then routed to that bean.
7 messages in order. Play them, or step through them one at a time.
The fragment is found by name and wired into the same proxy as the generated methods.
6The Postfix Setting and Common Mistakes
The Impl postfix is a default, not a rule. @EnableJpaRepositories(repositoryImplementationPostfix = "Fragment") changes it, and the class must then be called OrderRepositoryCustomFragment. Most projects keep the default, because every Spring developer recognises it.
The usual mistakes all show up at startup. If the class has any other name, such as OrderSearchImpl or OrderRepositoryCustomImplementation, Spring Data finds no implementation for search, tries to derive a query from the method name instead and fails at startup with an error that there is no property search found for type Order. (The older convention of naming one class after the repository itself, OrderRepositoryImpl, is still recognised for compatibility, but fragment-based naming is the recommended form.) If the class lives outside the scanned packages, the same error appears. And because fragments are plain beans, they do not get transactions automatically; put @Transactional on the implementation method or call it from a transactional service.
7Follow-up questions
Can one fragment be shared by several repositories?
Yes. A fragment such as SoftDeleteFragment<T> with an implementation SoftDeleteFragmentImpl can be extended by many repositories. Generic fragments need the implementation to work out the domain type, so many teams prefer a custom base repository class, configured with repositoryBaseClass, for behaviour every repository shares.
Can a fragment override a CRUD method such as save?
Yes. If the fragment declares a method with the same signature as an inherited one, the fragment's implementation takes precedence, because fragments are consulted before the base implementation. It is a powerful tool, so use it sparingly and document why.
Can the implementation call other methods of the same repository?
It can inject the repository lazily, but that creates a circular dependency and confusing code. Usually it is better for the implementation to use the EntityManager directly, or to move the combined logic into a service.
8Conclusion
In this answer, we saw that a custom repository method lives in a fragment: an interface such as OrderRepositoryCustom and a class named OrderRepositoryCustomImpl that injects the EntityManager and builds the query with Criteria or JPQL. The repository extends both JpaRepository and the fragment, and Spring Data finds the implementation by its name, interface plus Impl, in the scanned packages and combines it into the repository proxy, with repositoryImplementationPostfix available to change the postfix.
Open with
I would use a repository fragment. First a fragment interface, OrderRepositoryCustom, with just the custom method, for example List<Order> search(SearchCriteria c). It does not extend Repository.
Then, if they want more
Then the implementation, and the name matters: it must be the fragment interface name plus the Impl postfix, so OrderRepositoryCustomImpl.
It is an ordinary Spring bean, so I inject the EntityManager through the constructor and build the query, here with the Criteria API: get the CriteriaBuilder, create a CriteriaQuery for Order, and add a predicate for each criterion that is present, status equals, total at least the minimum, customer name like the input, then order by createdAt and cap the results.
JPQL with named parameters works too, but Criteria avoids string concatenation when conditions vary. Finally, OrderRepository extends both JpaRepository<Order, Long> and OrderRepositoryCustom, so callers inject one repository and see CRUD, derived methods and search together. How does Spring find it?
While creating the repository, Spring Data looks at each extended interface that is not one of its own, searches the scanned repository packages for a class with that name plus Impl, registers it as a bean, and adds it as a fragment of the proxy, so calls to search are routed to my class.
The postfix can be changed with repositoryImplementationPostfix on @EnableJpaRepositories. The common mistake is a wrong class name or package; then Spring Data tries to derive a query from the method name and fails at startup saying there is no property search on Order. And fragments do not get transactions automatically, so I mark the method or the calling service @Transactional.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Add a custom method with hand-written JPQL or Criteria code to a Spring Data repository....": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/b3cb45991d/