What if you could manage all your data with just a few simple commands instead of writing complex code every time?
Why CRUD methods (save, findById, findAll, delete) in Spring Boot? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web app where you must write separate code to add, find, list, and remove user data from a database every time.
You have to write SQL queries manually and handle database connections yourself.
Writing all database operations manually is slow and error-prone.
It's easy to forget to close connections or write wrong queries, causing bugs and crashes.
Maintaining this code as your app grows becomes a nightmare.
CRUD methods like save, findById, findAll, and delete in Spring Boot simplify database work.
They provide ready-made functions to add, find, list, and remove data safely and quickly.
This means less code, fewer bugs, and faster development.
Connection conn = DriverManager.getConnection(...);
Statement stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO users ...");
conn.close();userRepository.save(user); User user = userRepository.findById(id).orElse(null); List<User> users = userRepository.findAll(); userRepository.delete(user);
It enables developers to focus on app features instead of database details, making apps faster and more reliable.
When you sign up on a website, the app uses save() to store your info, findById() to get your profile, findAll() to list users for admin, and delete() to remove accounts.
Manual database code is complex and error-prone.
CRUD methods provide simple, reusable database operations.
They speed up development and reduce bugs.
Practice
Solution
Step 1: Understand the purpose of
Thesave()save()method is designed to add a new entity or update an existing one in the database.Step 2: Compare with other methods
findById()andfindAll()are for reading data, anddelete()is for removing data, so they do not add or update.Final Answer:
save()-> Option AQuick Check:
Add or update data =save()[OK]
- Confusing
save()withfindById() - Thinking
delete()adds data - Using
findAll()to save data
Solution
Step 1: Identify the method to find by ID
The methodfindById(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 AQuick Check:
Find by ID syntax = repository.findById(id) [OK]
- Using save() to find an entity
- Passing ID to findAll(), which takes no parameters
- Using delete() to find data
allUsers.size() return if the database has 3 user records?List<User> allUsers = userRepository.findAll(); int count = allUsers.size();
Solution
Step 1: Understand
ThefindAll()behaviorfindAll()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,allUserswill contain 3 elements, soallUsers.size()returns 3.Final Answer:
3 -> Option BQuick Check:
findAll() returns all records count = 3 [OK]
- Assuming findAll() returns null if no records
- Confusing size() with findById() result
- Thinking findAll() returns a single entity
userRepository.delete(5);
Assuming
delete expects an entity object, not an ID.Solution
Step 1: Understand
Thedeletemethod signaturedeletemethod expects an entity object, not just an ID.Step 2: Use correct method to delete by ID
To delete by ID, usedeleteById(id)method instead ofdelete(id).Final Answer:
You should usedeleteById(5)instead to delete by ID. -> Option DQuick Check:
Delete by ID = deleteById(id) [OK]
- Passing ID to delete() instead of entity
- Assuming delete() accepts ID directly
- Using wrong data type for ID
// Assume userId and newEmail are given
Optional<User> userOpt = userRepository.findById(userId);
if (userOpt.isPresent()) {
User user = userOpt.get();
user.setEmail(newEmail);
// What next?
}Solution
Step 1: Retrieve the user entity by ID
The code usesfindById(userId)to get the user object.Step 2: Update the user and save changes
After setting the new email, callingsave(user)updates the database record.Final Answer:
CalluserRepository.save(user);to update the user. -> Option CQuick Check:
Update data = findById + modify + save [OK]
- Assuming changes auto-save without calling save()
- Deleting before saving to update
- Using findAll() to update a single record
