Fine-tuning Repository

A spring data Jpa repository usually extends Repository, CrudRepository, or PagingAndSortingRepository. But extending these interfaces provides access to all the repositries including delete as well which may pose a security or data integrity issue. Inheriting unwanted methods also makes the Repository interface heavy as well. If only save method is required then extending all the methods offer by Spring data Jpa is redundant. To avoid this situation, Spring Data Jpa provides 2 ways.

  • Copy required methods from CrudRepository into domain repository.
  • limit exposed method via @RepositoryDefinition.

Spring Data Jpa provides mechanism to define own abstractions on top of the provided Spring Data Repositories functionality.

follow us on