Chapter 14: Implementing data persistence with Spring Data
Introduces the persistence layer and explains how Spring Data reduces the code needed to work with database data.
After this chapter you will be able to
- Spring Data Repository Interfaces. Explain that with Spring Data, developers declare repository interfaces instead of writing repository classes themselves; Spring provides implementations at runtime, reducing repeated database-access code.
- Spring Data common abstractions. Describe how Spring Data lets developers write persistence operations similarly across technologies and why that reduces the code they must write.
- Spring Data modules. Explain that Spring Data is an umbrella project with independent modules for different persistence technologies, each added through its own Maven dependency.
- Common repository contracts. Describe what an app gains by extending Repository, CrudRepository, or PagingAndSortingRepository.
- Contract selection rationale. Justify choosing a repository contract that supplies only the operations an app needs, such as CrudRepository for CRUD alone or PagingAndSortingRepository when paging and sorting are also needed.
- Repository contract and implementation. Explain how AccountRepository obtains common data operations without a handwritten implementation and how the service receives it.
- Method name query derivation. Trace how Spring Data interprets findAccountsByName, or findByName, to retrieve accounts for a given name.
- Explicit custom query choice. Justify the section’s preference for @Query over method-name derivation and explain what @Query changes.
- Transactional money transfer flow. Trace how the service uses the repository for a transfer, why the work is transactional, and how the example checks the result.
What it builds on
- What a data source is (chapter 12) · The earlier discussion of how Spring apps use data sources.
- Using JdbcTemplate to work with persisted data (chapter 12) · Earlier work with database data using JdbcTemplate.
- Using interfaces for decoupling implementations (chapter 4) · The idea of using interfaces as abstractions.
- Using stereotype annotations to create beans (chapter 2) · The use of stereotype annotations to add beans to the Spring context.
- Using @Autowired to inject the values through the constructor (chapter 3) · injecting a bean through a constructor
- Using transactions in Spring apps (chapter 13) · using @Transactional in a Spring application
Section by section
Introduction
Introduces the persistence layer and explains how Spring Data reduces the code needed to work with database data.
- Persistence Layer Role: Describe the persistence layer as the part of a Spring application that talks to the database to store, read, change, and delete data.
Why it matters: It identifies the part of the application that Spring Data helps you implement. - Spring Data Repository Interfaces: Explain that with Spring Data, developers declare repository interfaces instead of writing repository classes themselves; Spring provides implementations at runtime, reducing repeated database-access code.
Why it matters: It explains the main benefit of using Spring Data for an application's persistence layer.
Key terms: persistence layer, Spring Data, repository interfaces, implementations at runtime
14.1 What Spring Data is
This section explains why varied persistence technologies make repository development complex and how Spring Data simplifies it through common abstractions and provided implementations.
- Persistence technology variety: Explain why implementing an app’s persistence layer can become complex when different technologies, and even different approaches within one technology, require different repository implementations.
Why it matters: The variety of persistence approaches explains the problem Spring Data is meant to simplify. - Spring Data common abstractions: Describe how Spring Data lets developers write persistence operations similarly across technologies and why that reduces the code they must write.
Why it matters: This is the central reason Spring Data can simplify repository development across persistence technologies.
Key terms: persistence technology, persistence layer, repositories, JDBC, JdbcTemplate, Hibernate, Spring Data, abstractions, interfaces, implementations
14.2 How Spring Data works
The section explains how Spring Data modules provide persistence support through a hierarchy of repository interfaces, including technology-specific contracts.
- Spring Data modules: Explain that Spring Data is an umbrella project with independent modules for different persistence technologies, each added through its own Maven dependency.
Why it matters: Choosing a persistence technology also determines which Spring Data module your app needs. - Common repository contracts: Describe what an app gains by extending Repository, CrudRepository, or PagingAndSortingRepository.
Why it matters: The interface an app extends determines which predefined persistence operations it gets. - Contract selection rationale: Justify choosing a repository contract that supplies only the operations an app needs, such as CrudRepository for CRUD alone or PagingAndSortingRepository when paging and sorting are also needed.
Why it matters: Selecting the narrower contract keeps a repository’s available operations aligned with the app’s needs. - Technology-specific repository contracts: Explain that particular Spring Data modules can offer repository contracts with operations suited to their technology, while an app needing only CRUD can still use CrudRepository.
Why it matters: A technology-specific contract is an option when its extra operations are useful, not a requirement for basic CRUD. - Repository annotation distinction: Distinguish the @Repository stereotype annotation from the Spring Data Repository interface.
Why it matters: The similar names refer to different tools with different roles.
Key terms: Spring Data, module, Maven dependency, Spring Data JDBC, Repository, CrudRepository, PagingAndSortingRepository, marker interface, CRUD, paging
14.3 Using Spring Data JDBC
This section builds a Spring Data JDBC repository for an electronic wallet and uses it to transfer money between accounts.
- Account table and model: Describe how the example prepares account data and maps it to an Account class, including how Spring Data identifies the primary key.
Why it matters: The repository needs a table and a model whose primary key it can identify. - Repository contract and implementation: Explain how AccountRepository obtains common data operations without a handwritten implementation and how the service receives it.
Why it matters: A repository interface supplies routine persistence operations while leaving the service focused on the application’s work. - Method name query derivation: Trace how Spring Data interprets findAccountsByName, or findByName, to retrieve accounts for a given name.
Why it matters: It explains how a custom lookup can work even when its SQL is not written explicitly. - Explicit custom query choice: Justify the section’s preference for @Query over method-name derivation and explain what @Query changes.
Why it matters: Explicit SQL makes the operation’s behavior less dependent on a specially constructed method name. - Data changing repository queries: State how a custom repository query that changes data is marked, as distinct from specifying its SQL.
Why it matters: A transfer needs repository operations that change balances, not just read them. - Transactional money transfer flow: Trace how the service uses the repository for a transfer, why the work is transactional, and how the example checks the result.
Why it matters: The example connects repository operations to a complete use case that must keep both account balances consistent.
Key terms: H2 in-memory database, schema.sql, data.sql, Account, @Id, AccountRepository, CrudRepository, dynamic implementation, bean, findAll()
14.4 Questions
This section provides review questions on Spring Data concepts, with answers in Appendix A.
14.5 Summary
This summary explains how Spring Data uses technology-specific modules and repository interfaces to provide persistence operations, including custom queries.
Where this is used later
- Implementing unit tests in Spring apps (chapter 15) · The money-transfer use case provides the service and repository example being tested.
- Implementing Spring integration tests (chapter 16) · Assumes familiarity with the app’s repository and persisted data.
Concepts explained in this chapter
- CrudRepositoryCrudRepository is a Spring Data repository interface that declares common create, retrieve, update, and delete operations for a model type.
- @Modifying annotationThe @Modifying annotation marks a Spring Data repository query method as performing a data-changing operation rather than only retrieving data.
- @QueryThe @Query annotation declares the query expression that Spring Data uses to implement a repository method.
- @IdThe @Id annotation identifies the property or field that uniquely identifies a persistent entity.
- custom repository operationA custom repository operation is a repository method that performs a data-access action beyond the repository's inherited standard operations.
- derived query methodQuery derivation is the technique of generating a database query by interpreting a repository method's name according to framework naming rules.
- PagingAndSortingRepositoryPagingAndSortingRepository is a Spring Data repository interface that adds paging and sorting operations to CRUD repository operations.
- Spring DataSpring Data is a family of Spring projects that simplifies access to data stores through consistent repository-based abstractions.
- data layerA persistence layer is a software layer responsible for storing and retrieving application data beyond the lifetime of a process.
- Interface Segregation PrincipleInterface segregation is a design principle that favors several focused interfaces over one interface containing operations that clients do not need.
- JpaRepositoryJpaRepository is a Spring Data JPA repository interface that provides persistence operations for Jakarta Persistence API entities.
- Spring Data JDBCSpring Data JDBC is a Spring Data module that provides repository-based access to relational databases through JDBC.
- abstractionAn abstraction is a representation that separates the behavior an object requires from the details of how that behavior is implemented.
- repository patternA repository is an abstraction that provides access to stored application data.
- interfaceA Java interface is a reference type that declares a responsibility or contract whose behavior must be provided by implementing classes.
- object-relational mapperAn ORM framework provides software for mapping application objects to relational database records and relationships.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
