Master Spring Data JPA to simplify database operations in Spring applications. Learn to use repository interfaces (CrudRepository
and JpaRepository
) for CRUD operations, implement query methods (derived and custom), and integrate Spring Data JPA with Spring Boot to build a web application with persistent data storage using a relational database (e.g., MySQL) and Thymeleaf for the view layer.
Definition: Spring Data JPA is a sub-module of Spring Data that provides a repository-based abstraction over JPA (Java Persistence API), typically using Hibernate as the JPA provider, to simplify database access in relational databases.
Purpose:
Key Features:
DataAccessException
).Dependencies (for Spring Boot):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
Benefits:
CrudRepository<T, ID>:
Provides basic CRUD operations for an entity type T
with ID type ID
.
Key methods:
save(T entity)
: Saves or updates an entity.findById(ID id)
: Retrieves an entity by ID (returns Optional<T>
).findAll()
: Retrieves all entities.deleteById(ID id)
: Deletes an entity by ID.count()
: Returns the number of entities.Example:
public interface BookRepository extends CrudRepository<Book, Integer> {}
JpaRepository<T, ID>:
Extends CrudRepository
with additional features like pagination, sorting, and batch operations.
Additional methods:
findAll(Sort sort)
: Retrieves entities with sorting.findAll(Pageable pageable)
: Retrieves entities with pagination.deleteAll()
: Deletes all entities.flush()
: Synchronizes changes to the database.Example:
public interface BookRepository extends JpaRepository<Book, Integer> {}
Comparison:
CrudRepository
: Basic CRUD, minimal functionality.JpaRepository
: Enhanced with pagination, sorting, and JPA-specific features.Exam Tip: Know the key methods of both interfaces and when to use each.
Derived Queries:
Spring Data JPA generates SQL queries from method names in repository interfaces.
Naming conventions:
findBy[Property]
: Matches a property (e.g., findByTitle
).findBy[Property]And[Property]
: Combines conditions (e.g., findByTitleAndAuthor
).findBy[Property]Like
: Pattern matching (e.g., findByTitleLike
).findBy[Property]OrderBy[Property]
: Sorts results (e.g., findByAuthorOrderByTitleAsc
).Example:
public interface BookRepository extends JpaRepository<Book, Integer> {
List<Book> findByAuthor(String author);
List<Book> findByTitleLike(String titlePattern);
List<Book> findByAuthorOrderByTitleAsc(String author);
}
Pagination and Sorting:
Use Pageable
and Sort
for paginated and sorted results.
Example:
Page<Book> findAll(Pageable pageable);
// Usage in service
Pageable pageable = PageRequest.of(0, 10, Sort.by("title").ascending());
Page<Book> page = bookRepository.findAll(pageable);
Exam Tip: Be able to write derived query methods and explain how Spring Data generates SQL from them.