Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
CRUD methods (save, findById, findAll, delete) in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to manage a list of books in a library. Each book has an ID, title, and author.We want to create basic CRUD (Create, Read, Update, Delete) methods to save a book, find a book by its ID, find all books, and delete a book by its ID.
🎯 Goal: Build a Spring Boot repository interface with CRUD methods: save, findById, findAll, and deleteById for managing books.
📋 What You'll Learn
Create a Book entity class with fields id, title, and author
Create a BookRepository interface extending JpaRepository
Use save method to add or update a book
Use findById method to get a book by its ID
Use findAll method to get all books
Use deleteById method to remove a book by its ID
💡 Why This Matters
🌍 Real World
Managing data records in applications like libraries, stores, or user management systems requires CRUD operations to create, read, update, and delete data.
💼 Career
Understanding CRUD methods with Spring Boot and JPA is essential for backend developers working on Java web applications and APIs.
Progress0 / 4 steps
1
Create the Book entity class
Create a Java class called Book in package com.example.library with private fields Long id, String title, and String author. Add @Entity annotation and use @Id and @GeneratedValue on the id field.
Spring Boot
Hint
Use @Entity on the class and @Id with @GeneratedValue on the id field.
2
Create the BookRepository interface
Create an interface called BookRepository in package com.example.library that extends JpaRepository<Book, Long>.
Spring Boot
Hint
Extend JpaRepository with Book as entity and Long as ID type.
3
Use save and findById methods
In a service class called BookService in package com.example.library, inject BookRepository and write a method saveBook(Book book) that calls bookRepository.save(book). Also write a method findBookById(Long id) that returns bookRepository.findById(id).
Spring Boot
Hint
Inject BookRepository via constructor and use its save and findById methods.
4
Add findAll and deleteById methods
In the BookService class, add a method findAllBooks() that returns bookRepository.findAll(). Also add a method deleteBookById(Long id) that calls bookRepository.deleteById(id).
Spring Boot
Hint
Use findAll() to get all books and deleteById(id) to remove a book by ID.
Practice
(1/5)
1. Which Spring Boot repository method is used to add a new entity or update an existing one in the database?
easy
A. save()
B. findById()
C. delete()
D. findAll()
Solution
Step 1: Understand the purpose of save()
The save() method is designed to add a new entity or update an existing one in the database.
Step 2: Compare with other methods
findById() and findAll() are for reading data, and delete() is for removing data, so they do not add or update.
Final Answer:
save() -> Option A
Quick Check:
Add or update data = save() [OK]
Hint: Remember: save = add or update data [OK]
Common Mistakes:
Confusing save() with findById()
Thinking delete() adds data
Using findAll() to save data
2. Which of the following is the correct syntax to find an entity by its ID using Spring Boot's repository?
easy
A. repository.findById(id);
B. repository.save(id);
C. repository.delete(id);
D. repository.findAll(id);
Solution
Step 1: Identify the method to find by ID
The method findById(id) is used to retrieve an entity by its ID.
Step 2: Check syntax correctness
repository.findById(id); is the correct syntax. Other methods do not accept an ID to find an entity.
Final Answer:
repository.findById(id); -> Option A
Quick Check:
Find by ID syntax = repository.findById(id) [OK]
Hint: Use findById(id) to get one entity by its ID [OK]
Common Mistakes:
Using save() to find an entity
Passing ID to findAll(), which takes no parameters
Using delete() to find data
3. Given the following code snippet, what will allUsers.size() return if the database has 3 user records?
List<User> allUsers = userRepository.findAll();
int count = allUsers.size();
medium
A. 0
B. 3
C. 1
D. null
Solution
Step 1: Understand findAll() behavior
The findAll() method returns a list of all entities in the database.
Step 2: Count the number of records returned
If the database has 3 user records, allUsers will contain 3 elements, so allUsers.size() returns 3.
Final Answer:
3 -> Option B
Quick Check:
findAll() returns all records count = 3 [OK]
Hint: findAll() returns all records as a list [OK]
Common Mistakes:
Assuming findAll() returns null if no records
Confusing size() with findById() result
Thinking findAll() returns a single entity
4. What is wrong with this code snippet if it throws a compilation error?
userRepository.delete(5);
Assuming delete expects an entity object, not an ID.
medium
A. The ID must be a string, not an integer.
B. The method delete does not exist in Spring Boot repositories.
C. You must pass an entity object, not an ID, to delete.
D. You should use deleteById(5) instead to delete by ID.
Solution
Step 1: Understand delete method signature
The delete method expects an entity object, not just an ID.
Step 2: Use correct method to delete by ID
To delete by ID, use deleteById(id) method instead of delete(id).
Final Answer:
You should use deleteById(5) instead to delete by ID. -> Option D
Quick Check:
Delete by ID = deleteById(id) [OK]
Hint: Use deleteById(id) to remove by ID, not delete(entity) [OK]
Common Mistakes:
Passing ID to delete() instead of entity
Assuming delete() accepts ID directly
Using wrong data type for ID
5. You want to update a user's email in the database using Spring Boot. Which sequence of repository methods correctly achieves this?
// Assume userId and newEmail are given
Optional<User> userOpt = userRepository.findById(userId);
if (userOpt.isPresent()) {
User user = userOpt.get();
user.setEmail(newEmail);
// What next?
}
hard
A. Call userRepository.delete(user); then save(user);.
B. Call userRepository.findAll(); to refresh the list.
C. Call userRepository.save(user); to update the user.
D. No need to call any method; changes auto-save.
Solution
Step 1: Retrieve the user entity by ID
The code uses findById(userId) to get the user object.
Step 2: Update the user and save changes
After setting the new email, calling save(user) updates the database record.
Final Answer:
Call userRepository.save(user); to update the user. -> Option C
Quick Check:
Update data = findById + modify + save [OK]
Hint: Update entity fields then call save() to persist changes [OK]