balteo
balteo

Reputation: 24679

Understanding Spring Data JPA repositories common to all entities

I am in reference to the following Spring documentation: http://static.springsource.org/spring-data/data-jpa/docs/1.1.0.RELEASE/reference/html/#repositories.custom-behaviour-for-all-repositories

I am trying to understand the logic behind Jpa custom repositories as described above.

My guess is that I have to create a UserRepository interface (in my case User is the specific entity) extending the MyRepository interface and then supply an implementation for it with the proper parameter types. I then have one repository per entity each sharing a number of common methods defined in the MyRepository interface.

This raises a few questions:

One: What if I am performing operations across several entities: which repository do I use? Two: Does Spring Data Jpa allow for repositories without any type parameters?

Upvotes: 2

Views: 2041

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

What if I am performing operations across several entities: which repository do I use?

induces "DAO per entity" approach (not necessarily the best one). If you need to perform operations across several entities, use service layer for that.

Does Spring Data Jpa allow for repositories without any type parameters?

No, can you suggest some use cases for that? Each DAO should provide strong, type safe interface for one entity. If you need more flexible DAO, maybe you should explore inheritance in JPA?

Upvotes: 3

Related Questions