Bird
Raised Fist0
Spring Bootframework~20 mins

CRUD methods (save, findById, findAll, delete) in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Spring Data CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does the save method do in a Spring Data repository?

Consider a Spring Data JPA repository interface. What is the main behavior of the save method?

AIt inserts a new entity or updates an existing one in the database.
BIt only inserts a new entity and throws an error if the entity exists.
CIt deletes the entity from the database.
DIt retrieves an entity by its ID.
Attempts:
2 left
💡 Hint

Think about whether save can handle both new and existing data.

state_output
intermediate
2:00remaining
What is the output of findById when the entity is not found?

Given a Spring Data repository, what does findById return if no entity matches the given ID?

Spring Boot
Optional<Entity> result = repository.findById(999L);
AAn empty <code>Optional</code> object.
BA <code>null</code> value.
CThrows <code>NoSuchElementException</code> immediately.
DReturns a default entity instance.
Attempts:
2 left
💡 Hint

Remember how Optional works in Java.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly deletes an entity by ID using Spring Data repository?

Choose the correct way to delete an entity by its ID using a Spring Data repository.

Arepository.deleteEntityById(entityId);
Brepository.deleteById(entityId);
Crepository.delete(entityId);
Drepository.remove(entityId);
Attempts:
2 left
💡 Hint

Check the official Spring Data repository method names.

🔧 Debug
advanced
2:00remaining
Why does this findAll call cause a runtime error?

Given this code snippet, why does calling findAll cause a runtime error?

List list = repository.findAll();
Spring Boot
List<Entity> list = repository.findAll();
AThe <code>findAll</code> method returns a Stream, not a List.
BThe <code>findAll</code> method requires an argument specifying a filter.
CThe entity class is missing a no-argument constructor.
DThe repository interface does not extend <code>JpaRepository</code> or <code>CrudRepository</code>.
Attempts:
2 left
💡 Hint

JPA entities require a no-argument constructor for instantiation.

🧠 Conceptual
expert
2:00remaining
What happens if you call deleteById with a non-existing ID?

In Spring Data JPA, what is the behavior when deleteById is called with an ID that does not exist in the database?

AIt silently does nothing without throwing an error.
BIt creates a new entity with that ID and then deletes it.
CIt returns a boolean indicating success or failure.
DIt throws <code>EmptyResultDataAccessException</code> immediately.
Attempts:
2 left
💡 Hint

Think about how Spring Data handles deleting non-existing records.

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

  1. 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.
  2. 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.
  3. Final Answer:

    save() -> Option A
  4. 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

  1. Step 1: Identify the method to find by ID

    The method findById(id) is used to retrieve an entity by its ID.
  2. Step 2: Check syntax correctness

    repository.findById(id); is the correct syntax. Other methods do not accept an ID to find an entity.
  3. Final Answer:

    repository.findById(id); -> Option A
  4. 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

  1. Step 1: Understand findAll() behavior

    The findAll() method returns a list of all entities in the database.
  2. 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.
  3. Final Answer:

    3 -> Option B
  4. 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

  1. Step 1: Understand delete method signature

    The delete method expects an entity object, not just an ID.
  2. Step 2: Use correct method to delete by ID

    To delete by ID, use deleteById(id) method instead of delete(id).
  3. Final Answer:

    You should use deleteById(5) instead to delete by ID. -> Option D
  4. 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

  1. Step 1: Retrieve the user entity by ID

    The code uses findById(userId) to get the user object.
  2. Step 2: Update the user and save changes

    After setting the new email, calling save(user) updates the database record.
  3. Final Answer:

    Call userRepository.save(user); to update the user. -> Option C
  4. Quick Check:

    Update data = findById + modify + save [OK]
Hint: Update entity fields then call save() to persist changes [OK]
Common Mistakes:
  • Assuming changes auto-save without calling save()
  • Deleting before saving to update
  • Using findAll() to update a single record