Core concepts
Core concepts
The central interface in the Spring Data repository abstraction is Repository . It takes the
domain class to manage as well as the identifier type of the domain class as type
arguments. This interface acts primarily as a marker interface to capture the types to work
with and to help you to discover interfaces that extend this one.
Spring Data considers domain types to be entities, more specifically aggregates. So you will see
the term "entity" used throughout the documentation that can be interchanged with the term
"domain type" or "aggregate".
As you might have noticed in the introduction it already hinted towards domain-driven concepts.
We consider domain objects in the sense of DDD. Domain objects have identifiers (otherwise
these would be identity-less value objects), and we somehow need to refer to identifiers when
working with certain patterns to access data. Referring to identifiers will become more
meaningful as we talk about repositories and query methods.
CrudRepository Interface
JAVA
public interface CrudRepository<T, ID> extends Repository<T, ID> {
Iterable<T> findAll(); 3
long count(); 4
The methods declared in this interface are commonly referred to as CRUD methods.
ListCrudRepository offers equivalent methods, but they return List where the
CrudRepository methods return an Iterable .
The repository interface implies a few reserved methods like findById(ID identifier) that
target the domain type identifier property regardless of its property name. Read more about this
in “Defining Query Methods”.
You can annotate your query method with @Query to provide a custom query if a property
named Id doesn’t refer to the identifier. Following that path can easily lead to confusion and is
discouraged as you will quickly hit type limits if the ID type and the type of your Id property
deviate.
PagingAndSortingRepository interface
JAVA
public interface PagingAndSortingRepository<T, ID> {
To access the second page of User by a page size of 20, you could do something like the
following:
JAVA
PagingAndSortingRepository<User, Long> repository = // … get access to a b
Page<User> users = repository.findAll(PageRequest.of(1, 20));
In addition to query methods, query derivation for both count and delete queries is
available. The following list shows the interface definition for a derived count query:
The following listing shows the interface definition for a derived delete query: