save method do in a Spring Data repository?Consider a Spring Data JPA repository interface. What is the main behavior of the save method?
Think about whether save can handle both new and existing data.
The save method in Spring Data repositories either inserts a new entity if it doesn't exist or updates it if it does. It does not delete or retrieve entities.
findById when the entity is not found?Given a Spring Data repository, what does findById return if no entity matches the given ID?
Optional<Entity> result = repository.findById(999L);Remember how Optional works in Java.
The findById method returns an Optional. If the entity is not found, the Optional is empty, not null or an exception.
Choose the correct way to delete an entity by its ID using a Spring Data repository.
Check the official Spring Data repository method names.
The correct method to delete by ID is deleteById. Other methods like remove or deleteEntityById do not exist in the standard repository interface.
findAll call cause a runtime error?Given this code snippet, why does calling findAll cause a runtime error?
Listlist = repository.findAll();
List<Entity> list = repository.findAll();JPA entities require a no-argument constructor for instantiation.
JPA/Hibernate requires a no-argument constructor on entity classes to instantiate objects from database results. Without it, findAll throws a runtime exception (e.g., InstantiationException) when mapping entities.
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?
Think about how Spring Data handles deleting non-existing records.
Calling deleteById with a non-existing ID throws EmptyResultDataAccessException to indicate no entity was found to delete.