Archimedes Trajano
Archimedes Trajano

Reputation: 41290

Multiple Spring Data CrudRepository in single interface

Instead of creating one specialization of a CrudRepository, can multiple Spring Data JPA CrudRepository in single interface

AND

Still have @NamedQueriesbe referenced from the interface?

At the moment, I am creating a separate facade class that would collect all the repositories and expose a more unified API, but it's mostly forwarding.

Upvotes: 0

Views: 385

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36133

That's not how Spring Data JPA repository works:

4.3. Defining Repository Interfaces

First, define a domain class-specific repository interface.

The interface must extend Repository and be typed to the domain class and an ID type. If you want to expose CRUD methods for that domain type, extend CrudRepository instead of Repository

Source: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.definition

If you don't like this approach you can go ahead and implement your own generic Repository.

How this can be done you can see in the default implementation SimpleJpaRepository:

https://github.com/spring-projects/spring-data-jpa/blob/master/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Upvotes: 1

Related Questions