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
Recall & Review
beginner
What does the save method do in Spring Boot's CRUD repository?
The save method adds a new entity or updates an existing one in the database. It returns the saved entity with any generated values like IDs.
Click to reveal answer
beginner
How does findById work in Spring Boot repositories?
It looks for an entity by its unique ID. It returns an Optional that contains the entity if found, or is empty if not found.
Click to reveal answer
beginner
What is the purpose of findAll in Spring Boot CRUD repositories?
It retrieves all entities of a given type from the database and returns them as a list.
Click to reveal answer
beginner
Explain what delete does in Spring Boot CRUD repositories.
The delete method removes an entity from the database. You can delete by passing the entity or by its ID using deleteById.
Click to reveal answer
intermediate
Why is Optional used with findById in Spring Boot?
Optional helps safely handle the case when an entity might not exist. It avoids errors by forcing you to check if the entity is present before using it.
Click to reveal answer
Which CRUD method in Spring Boot is used to add or update an entity?
Asave
BfindById
CfindAll
Ddelete
✗ Incorrect
The save method adds a new entity or updates an existing one.
What does findById return if no entity is found?
AAn empty Optional
BAn empty list
CAn exception
Dnull
✗ Incorrect
findById returns an empty Optional if the entity is not found.
Which method retrieves all entities from the database?
Asave
BfindAll
Cdelete
DfindById
✗ Incorrect
findAll returns a list of all entities.
How can you delete an entity using Spring Boot CRUD repository?
AOnly by entity ID
BOnly by entity object
CBy calling save with null
DBy entity object or ID
✗ Incorrect
You can delete by passing either the entity object or its ID.
Why is it good to use Optional with findById?
ATo automatically delete missing entities
BTo speed up queries
CTo avoid null pointer errors
DTo return multiple entities
✗ Incorrect
Optional helps avoid null pointer errors by forcing checks for presence.
Describe how the save, findById, findAll, and delete methods work in Spring Boot CRUD repositories.
Think about how you add, find, list, and remove items in a real collection.
You got /4 concepts.
Explain why Optional is useful when using findById in Spring Boot.
Consider what happens if you look for something that might not be there.
You got /4 concepts.
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]