• Now all data be ready for publish, We had made our blog alive now. Haha.

  • These days CCP GFW blocked all the IPs from US and when I switch my IP in google Cloud, the disk data cannot be restore again. I have to restart my blog totally, Now the website is still building…

Using generics in Spring Data JPA repositories- Stack Overflow

Java PingBook 1 months ago (10-23) 14 0


I have a number of simple object types that need to be persisted to a database. I am using Spring JPA to manage this persistence. For each object type I need to build the following:

import org.springframework.data.jpa.repository.JpaRepository;

public interface FacilityRepository extends JpaRepository {

public interface FacilityService {
public Facility create(Facility facility);

public class FacilityServiceImpl implements FacilityService {

private FacilityRepository countryRepository;

public Facility create(Facility facility) {
Facility created = facility;
return facilityRepository.save(created);

It occurred to me that it may be possible to replace the multiple classes for each object type with three generics based classes, thus saving a lot of boilerplate coding. I am not exactly sure how to go about it and in fact if it is a good idea?


First of all, I know we’re raising the bar here quite a bit but this is already tremendously less code than you had to write without the help of Spring Data JPA.

Second, I think you don’t need the service class in the first place, if all you do is forward a call to the repository. We recommend using services in front of the repositories if you have business logic that needs orchestration of different repositories within a transaction or has other business logic to encapsulate.

Generally speaking, you can of course do something like this:

interface ProductRepository extends CrudRepository {

@Query(“select p from #{#entityName} p where ?1 member of p.categories”)
Iterable findByCategory(String category);

Iterable findByName(String name);

This will allow you to use the repository on the client side like this:

class MyClient {

public MyClient(ProductRepository carRepository,
ProductRepository wineRepository) { … }

and it will work as expected. However there are a few things to notice:

This only works if the domain classes use single table inheritance. The only information about the domain class we can get at bootstrap time is that it will be Product objects. So for methods like findAll() and even findByName(…) the relevant queries will start with select p from Product p where…. This is due to the fact that the reflection lookup will never ever be able to produce Wine or Car unless you create a dedicated repository interface for it to capture the concrete type information.

Generally speaking, we recommend creating repository interfaces per aggregate root. This means you don’t have a repo for every domain class per se. Even more important, a 1:1 abstraction of a service over a repository is completely missing the point as well. If you build services, you don’t build one for every repository (a monkey could do that, and we’re no monkeys, are we? ;). A service is exposing a higher level API, is much more use-case drive and usually orchestrates calls to multiple repositories.

Also, if you build services on top of repositories, you usually want to enforce the clients to use the service instead of the repository (a classical example here is that a service for user management also triggers password generation and encryption, so that by no means it would be a good idea to let developers use the repository directly as they’d effectively work around the encryption). So you usually want to be selective about who can persist which domain objects to not create dependencies all over the place.


Yes, you can build generic repositories and use them with multiple domain types but there are quite strict technical limitations. Still, from an architectural point of view, the scenario you describe above shouldn’t even pop up as this means you’re facing a design smell anyway.

Copyright from PingBook Blog, If not specified, they are original. This site uses BY-NC-SAProtocol authenticated.
For reprinting, please indicate the link of the original text:Using generics in Spring Data JPA repositories- Stack Overflow
LIKE (0)
We create, We sharing! Tag every value data your sharing
Submit comments
Cancel comments
emoji picture bold strikethrough center italic check in

Hi,you need to provide your name and email adress!

  • Name (Required)
  • Email (Required)
  • Website