Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save an entity using Spring Data JPA.
Spring Boot
repository.[1](entity); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using findById instead of save
Using delete instead of save
✗ Incorrect
The save method stores or updates an entity in the database.
2fill in blank
mediumComplete the code to find an entity by its ID.
Spring Boot
Optional<Entity> result = repository.[1](id); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of findById
Using findAll instead of findById
✗ Incorrect
The findById method returns an Optional containing the entity if found.
3fill in blank
hardFix the error in the code to retrieve all entities.
Spring Boot
List<Entity> list = repository.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using findById which returns Optional
Using save which saves data
✗ Incorrect
The findAll method returns a list of all entities in the repository.
4fill in blank
hardFill both blanks to delete an entity by its ID.
Spring Boot
repository.[1]([2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using save instead of deleteById
Passing the wrong argument instead of the ID
✗ Incorrect
The deleteById method removes the entity with the given ID from the database.
5fill in blank
hardFill all three blanks to save an entity, then find it by ID and delete it.
Spring Boot
repository.[1](entity); Optional<Entity> found = repository.[2](entity.[3]());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using delete instead of findById
Using a wrong method to get the ID
✗ Incorrect
First, save stores the entity. Then, findById retrieves it by its ID using getId().