Skip to the content.

What is a Spring Data Repository interface?

An instant repository, also known as a Spring Data repository, is a repository that need no implementation and that supports the basic CRUD (create, read, update and delete) operations. Such a repository is declared as an interface that typically extend the Repository interface or an interface extending the Repository interface. Annotating an interface with @RepositoryDefinition will cause the same behaviour as extending Repository.

How do you define a Spring Data Repository interface? Why is it an interface not a class?

You have 2 options:

Repositories are defined as interfaces in order for Spring Data to be able to use the JDK dynamic proxy mechanism to create the proxy objects that intercept calls to repositories. With the Spring Data repository interfaces in place, annotate a Spring configuration class with an annotation @EnableJpaRepositories to enable the discovery and creation of repositories.

What is the naming convention for finder methods in a Spring Data Repository interface?

The naming convention of these finder methods are:

find(First[count])By[property expression][comparison operator][ordering operator]

How are Spring Data repositories implemented by Spring at runtime?

For a Spring Data repository a JDK dynamic proxy is created which intercepts all calls to the repository. The default behavior is to route calls to the default repository implementation, which in Spring Data JPA is the SimpleJpaRepository class.

What is @Query used for?

The @Query annotation allows for specifying a query to be used with a Spring Data JPA repository method.

@Query("select s from Spitter s where s.email like '%gmail.com'")
List<Spitter> findAllGmailSpitters();
@Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
User findByLastnameOrFirstname(@Param("lastname") String lastname,
                               @Param("firstname") String firstname);                   

References

  1. MrR0807 Spring certification notes
  2. Moss Green Spring certification notes
  3. Spring Documentation
  4. Spring Boot Documentation