Challenge - 5 Problems
JpaRepository Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this JpaRepository method call?
Given a JpaRepository for an entity
Product, what will be the result of calling findById(10L) if no product with ID 10 exists?Spring Boot
Optional<Product> product = productRepository.findById(10L);
System.out.println(product.isPresent());Attempts:
2 left
💡 Hint
Remember that findById returns an Optional, which may be empty if no entity is found.
✗ Incorrect
The findById method returns an Optional. If the entity is not found, the Optional is empty, so isPresent() returns false.
📝 Syntax
intermediate2:00remaining
Which option correctly declares a JpaRepository for an entity
User with primary key type Long?Choose the correct interface declaration for a JpaRepository managing
User entities with Long IDs.Attempts:
2 left
💡 Hint
JpaRepository is an interface and uses generics with entity type first, then ID type.
✗ Incorrect
The correct syntax uses extends for interfaces and the generic order is Entity, ID.
❓ state_output
advanced2:00remaining
What is the state of the database after calling
deleteById(5L) if no entity with ID 5 exists?Assuming
productRepository is a JpaRepository, what happens when productRepository.deleteById(5L) is called but no product with ID 5 exists?Attempts:
2 left
💡 Hint
Check the behavior of deleteById when the ID is not found.
✗ Incorrect
The deleteById method throws EmptyResultDataAccessException if the entity with the given ID does not exist.
🧠 Conceptual
advanced2:00remaining
Which JpaRepository method returns a paginated list of entities?
You want to retrieve entities in pages instead of all at once. Which JpaRepository method should you use?
Attempts:
2 left
💡 Hint
Look for a method that accepts a Pageable parameter.
✗ Incorrect
The findAll(Pageable pageable) method returns a Page of entities, allowing pagination.
🔧 Debug
expert3:00remaining
Why does this JpaRepository query method cause a runtime error?
Consider this method in a JpaRepository interface:
List findByAgeGreaterThan(int age); When called, it throws an exception. What is the most likely cause?Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByAgeGreaterThan(int age);
}Attempts:
2 left
💡 Hint
Check if the entity has the field used in the method name.
✗ Incorrect
Spring Data JPA derives queries from method names. If the entity lacks the field 'age', it cannot create the query, causing a runtime error.