The JpaRepository interface helps you easily manage database operations without writing SQL. It saves time by providing ready-made methods for common tasks like saving, finding, and deleting data.
0
0
JpaRepository interface in Spring Boot
Introduction
When you want to quickly add database access to your Spring Boot app.
When you need to perform basic CRUD (Create, Read, Update, Delete) operations on your data.
When you want to avoid writing repetitive SQL queries for common tasks.
When you want to use Spring Data JPA features like pagination and sorting easily.
When you want to focus on business logic instead of database code.
Syntax
Spring Boot
public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {
// custom query methods if needed
}YourEntity is the class representing your database table.
Long is the type of the primary key (ID) of your entity.
Examples
This example creates a repository for
User entities with an Integer ID. It also adds a method to find users by their last name.Spring Boot
public interface UserRepository extends JpaRepository<User, Integer> {
List<User> findByLastName(String lastName);
}This example shows a simple repository for
Product entities with Long IDs, using only built-in methods.Spring Boot
public interface ProductRepository extends JpaRepository<Product, Long> {
// No extra methods needed for basic CRUD
}Sample Program
This example defines a Book entity and a BookRepository that extends JpaRepository. It shows saving books and finding books by author using the repository methods.
Spring Boot
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import jakarta.persistence.*; import java.util.List; import org.springframework.stereotype.Service; @Entity public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; private String author; // Constructors, getters, setters public Book() {} public Book(String title, String author) { this.title = title; this.author = author; } public Long getId() { return id; } public String getTitle() { return title; } public String getAuthor() { return author; } public void setTitle(String title) { this.title = title; } public void setAuthor(String author) { this.author = author; } } @Repository public interface BookRepository extends JpaRepository<Book, Long> { List<Book> findByAuthor(String author); } // Usage in a service or controller @Service public class BookService { private final BookRepository bookRepository; public BookService(BookRepository bookRepository) { this.bookRepository = bookRepository; } public void demo() { Book book1 = new Book("Spring Boot Guide", "Alice"); Book book2 = new Book("Java Basics", "Bob"); bookRepository.save(book1); bookRepository.save(book2); List<Book> booksByAlice = bookRepository.findByAuthor("Alice"); booksByAlice.forEach(book -> System.out.println(book.getTitle())); } }
OutputSuccess
Important Notes
You don't need to implement JpaRepository methods yourself; Spring Data JPA does it automatically.
Use meaningful method names like findByAuthor to create custom queries without SQL.
Remember to annotate your repository interface with @Repository for Spring to detect it.
Summary
JpaRepository provides ready-to-use database methods for your entities.
It helps you avoid writing SQL and speeds up development.
Custom query methods can be added by following naming rules.