Junior — Middle
For what tasks is the CrudRepository interface intended in database work?
sobes.tech AI
Answer from AI
The CrudRepository interface in Java (usually in Spring Data) is designed to simplify working with a database by providing standard methods for basic CRUD operations (Create, Read, Update, Delete).
Main tasks of CrudRepository:
- Creating new records (save)
- Reading data by identifier or retrieving all records (findById, findAll)
- Updating existing records (save)
- Deleting records (deleteById, delete)
Using CrudRepository allows you to avoid writing SQL queries manually for these operations and work with entities through a convenient API.
Example:
public interface UserRepository extends CrudRepository<User, Long> {
// Additional methods can be declared here
}
Now you can call userRepository.save(user), userRepository.findById(id), etc.