Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Difference Between CrudRepository and JpaRepository

Both CrudRepository and JpaRepository are part of the Spring Data JPA project, and they provide a way to perform database operations on entities without the need for boilerplate code. However, there are notable differences between them:

  1. Hierarchy:

    • CrudRepository: This is a fundamental interface that provides CRUD (Create, Read, Update, Delete) functionality.
    • JpaRepository: This extends both CrudRepository and PagingAndSortingRepository. Thus, JpaRepository contains API for CRUD operations, paging and sorting operations, and some JPA-specific operations.
  2. Feature Set:

    • CrudRepository: Provides basic CRUD operations.
    • JpaRepository: Contains the full feature set of both CrudRepository and PagingAndSortingRepository. It adds JPA-related functionality, such as flushing the persistence context and deleting records in batches.
  3. Intended Datastore:

    • CrudRepository: While commonly used with JPA, the CrudRepository is more general and can be used with other Spring Data projects, like Spring Data JDBC, Spring Data MongoDB, etc.
    • JpaRepository: As its name implies, it's specific to JPA and will not work with other Spring Data projects that aren't JPA-based.
  4. Return Type of Save Method:

    • CrudRepository: The save(…) method returns the saved entity.
    • JpaRepository: It also returns the saved entity, but this behavior is inherited from CrudRepository.
  5. Batch Operations:

    • CrudRepository: Doesn't support batch operations by default.
    • JpaRepository: Provides the flush() and saveAndFlush(…) methods for batch operations, and methods like deleteInBatch(…) for batch deletes.
  6. Pagination:

    • CrudRepository: Does not support pagination directly.
    • JpaRepository: Inherits methods from PagingAndSortingRepository, allowing for easy pagination and sorting using Pageable and Sort parameters.

When you're working with Spring Boot and JPA, and you need to create a repository, you often have to decide between these interfaces. The decision typically hinges on the specific requirements of your application:

  • If you only need basic CRUD operations, CrudRepository might suffice.
  • If you require additional features like pagination, sorting, or JPA-specific features, then JpaRepository would be more appropriate.

Most Spring Boot applications that use JPA tend to lean towards JpaRepository due to the comprehensive set of features it offers out of the box.

  1. Custom queries and methods with CrudRepository and JpaRepository in Spring Boot:

    • Both repositories support custom query methods using method naming conventions.
    • Example (JpaRepository):
      public interface UserRepository extends JpaRepository<User, Long> {
          List<User> findByLastName(String lastName);
      }