Skip to content

Spring Data Repositories

This diagram shows the core repository hierarchy, starting from the base Repository marker interface up to the new, more specialized interfaces like ListCrudRepository.

flowchart TD
    A[Repository
Marker Interface] --> B[CrudRepository]; A --> C[PagingAndSortingRepository]; B --> D[ListCrudRepository
New: Returns List]; C --> E[ListPagingAndSortingRepository
New: Returns List]; B --> F[JpaRepository
JPA-specific methods]; F -.->|extends| C; F -.->|extends| B;

Key Takeaway: The new ListCrudRepository and ListPagingAndSortingRepository (introduced in recent Spring Data versions) are now the recommended starting points, returning List instead of Iterable.


This diagram illustrates the various ways to define queries in a Spring Data repository interface.

flowchart TD
    A[Developer Defines
Repository Interface] subgraph B [Spring Data's Query Lookup Strategies] direction LR B1["1. @Query Annotation
Explicit JPQL or SQL"] B2["2. Method Name Derivation
Parse method name into query"] B3["3. Named Query
Entity-linked query definition"] end A -- "Declares Methods" --> B B --> C{Spring Data JPA
Runtime Engine} C --> D[Creates & Executes
the JPA Query] D --> E[(Database)]

Key Takeaway: You don’t write the implementation. Spring Data JPA parses the method name or annotation at runtime to create the query.


3. Additional Repository Modules (MongoDB, etc.)

Section titled “3. Additional Repository Modules (MongoDB, etc.)”

This diagram shows how the Spring Data umbrella project provides a consistent programming model across different data stores through its module-specific repositories.

flowchart TD
    A[Spring Data Commons] --> B[Defines Core Interfaces
e.g., Repository, CrudRepository]; B --> C[Spring Data JPA]; B --> D[Spring Data MongoDB]; B --> E[Spring Data Redis]; B --> F[Spring Data Elasticsearch]; subgraph G [Module-Specific Repositories] C --> C1[JpaRepository]; D --> D1[MongoRepository]; E --> E1[RedisRepository]; F --> F1[ElasticsearchRepository]; end

Key Takeaway: The common interfaces from Spring Data Commons allow you to switch between different persistence technologies (e.g., from JPA to MongoDB) with minimal changes to your service layer code.


4. The Complete Spring Data JPA Architecture

Section titled “4. The Complete Spring Data JPA Architecture”

This diagram provides an overview of how all the components fit together, from your interface definition to the database.

flowchart TD
    A[Your Interface
interface UserRepository
extends JpaRepository
] --> B[JpaRepository]; subgraph C [Spring Data JPA] B --> D[SimpleJpaRepository
Default Implementation]; D --> E[Query Execution Engine]; E --> F[EntityManager]; end F --> G[JPA Provider
Hibernate / EclipseLink]; G --> H[(Database)]; C --o I[Persistence Layer]; G --o I; H --o I; J[Your Entity Class
@Entity class User] -.->|is managed by| F; J -.->|is mapped to| H;

1. @Query Annotation (Explicit & Most Powerful)

public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.email = ?1") // JPQL
// @Query(value = "SELECT * FROM users WHERE email = ?1", nativeQuery = true) // SQL
User findByEmailAddress(String email);
}

2. Method Name Derivation (Declarative & Concise)

public interface UserRepository extends JpaRepository<User, Long> {
// Parsed as: WHERE active = true AND lastName = :name
List<User> findByActiveTrueAndLastName(String name);
// Keywords: findBy, readBy, queryBy, getBy, existsBy, countBy, deleteBy/removeBy
// Supports: And, Or, Between, LessThan, Like, IgnoreCase, OrderBy
}

3. Named Query (Externalized Definition)

@Entity
@NamedQuery(name = "User.findByActiveStatus",
query = "SELECT u FROM User u WHERE u.active = ?1")
public class User { ... }
// In the repository interface
public interface UserRepository extends JpaRepository<User, Long> {
User findByActiveStatus(boolean active); // Automatically uses named query
}

The runtime engine evaluates these strategies in a specific order (typically @Query first, then Named Query, then Method Name parsing) to provide the implementation for your interface.

  • CrudRepository: Basic CRUD operations (save, findById, delete, etc.)
  • PagingAndSortingRepository: Adds pagination and sorting capabilities
  • JpaRepository: JPA-specific methods (flush, saveAndFlush, deleteInBatch)
  • Derived Query Methods: Auto-generated from method names
  • JPQL Queries: Custom queries using @Query annotation
  • Native Queries: SQL queries with nativeQuery = true
  • Projection Queries: Interface-based data projections
  • Reactive Repositories: For reactive programming support
  • MongoDB Repositories: MongoDB-specific repository support
  • Data REST Repositories: REST exposure with @RepositoryRestResource
  • @EnableJpaRepositories: Manual repository configuration
  • Auto-configuration: Spring Boot’s automatic setup

This hierarchy represents the repository structure available in Spring Boot 3.4 and later versions, showing the inheritance chain and various repository types supported.