Performance: CRUD methods (save, findById, findAll, delete)
This concept affects how quickly data operations respond and how much the UI waits for data changes, impacting user interaction speed and perceived responsiveness.
Jump into concepts and practice - no test required
Page<Entity> findAll(Pageable pageable) { return repository.findAll(pageable); } // fetches data in pagesList<Entity> findAll() { return repository.findAll(); } // fetches all data without pagination| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetch all without pagination | N/A | N/A | Blocks UI update waiting for data | [X] Bad |
| Fetch with pagination | N/A | N/A | Allows incremental UI updates | [OK] Good |
| Save without check | N/A | N/A | Causes unnecessary DB writes delaying response | [X] Bad |
| Save with existence check | N/A | N/A | Reduces DB writes improving response | [OK] Good |
| Delete without check | N/A | N/A | May cause errors and wasted DB calls | [X] Bad |
| Delete with existence check | N/A | N/A | Avoids errors and unnecessary DB load | [OK] Good |
save()save() method is designed to add a new entity or update an existing one in the database.findById() and findAll() are for reading data, and delete() is for removing data, so they do not add or update.save() -> Option Asave() [OK]save() with findById()delete() adds datafindAll() to save datafindById(id) is used to retrieve an entity by its ID.repository.findById(id); is the correct syntax. Other methods do not accept an ID to find an entity.allUsers.size() return if the database has 3 user records?List<User> allUsers = userRepository.findAll(); int count = allUsers.size();
findAll() behaviorfindAll() method returns a list of all entities in the database.allUsers will contain 3 elements, so allUsers.size() returns 3.userRepository.delete(5);
delete expects an entity object, not an ID.delete method signaturedelete method expects an entity object, not just an ID.deleteById(id) method instead of delete(id).deleteById(5) instead to delete by ID. -> Option D// Assume userId and newEmail are given
Optional<User> userOpt = userRepository.findById(userId);
if (userOpt.isPresent()) {
User user = userOpt.get();
user.setEmail(newEmail);
// What next?
}findById(userId) to get the user object.save(user) updates the database record.userRepository.save(user); to update the user. -> Option C