Challenge - 5 Problems
Service Repository Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when the service calls the repository method?
Consider a Spring Boot service class calling a repository method that returns an Optional. What will be the output if the repository returns Optional.empty() and the service calls
getUserNameById() which returns the user's name or 'Unknown' if not found?Spring Boot
public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public String getUserNameById(Long id) { return userRepository.findById(id) .map(User::getName) .orElse("Unknown"); } } public interface UserRepository { Optional<User> findById(Long id); } public record User(Long id, String name) {}
Attempts:
2 left
💡 Hint
Think about how Optional.orElse() works when the Optional is empty.
✗ Incorrect
The repository returns Optional.empty(), so map() is skipped and orElse("Unknown") returns the default string 'Unknown'.
📝 Syntax
intermediate2:00remaining
Which repository method signature is correct for Spring Data JPA?
You want to define a repository interface method to find a User by email. Which method signature is correct to let Spring Data JPA generate the query automatically?
Spring Boot
public interface UserRepository extends JpaRepository<User, Long> {
// Choose the correct method signature
}Attempts:
2 left
💡 Hint
Spring Data JPA supports Optional return types for nullable results.
✗ Incorrect
The correct signature is Optional findByEmail(String email); because it follows Spring Data naming conventions and handles absent results safely.
🔧 Debug
advanced2:00remaining
Why does the service fail to autowire the repository?
Given the service class below, why does Spring fail to inject the UserRepository bean?
Spring Boot
import org.springframework.stereotype.Service; @Service public class UserService { private final UserRepository userRepository; public UserService() { this.userRepository = null; } public String getUserName(Long id) { return userRepository.findById(id).map(User::getName).orElse("Unknown"); } }
Attempts:
2 left
💡 Hint
Spring injects dependencies via constructor or field injection. Check the constructor.
✗ Incorrect
The service defines a no-args constructor that sets userRepository to null, so Spring cannot inject the repository. A constructor with UserRepository parameter is needed.
❓ state_output
advanced2:00remaining
What is the state of the user list after service method call?
The service calls repository.saveAll() with a list of new users. What will be the size of the list returned by the service method?
Spring Boot
public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public List<User> saveUsers(List<User> users) { return userRepository.saveAll(users); } } public interface UserRepository { List<User> saveAll(List<User> users); }
Attempts:
2 left
💡 Hint
saveAll returns the saved entities, usually matching the input size.
✗ Incorrect
The repository's saveAll method returns a list of saved users matching the input list size unless an error occurs.
🧠 Conceptual
expert3:00remaining
What happens if a transactional service method calls a repository and an exception occurs?
In a Spring Boot application, a service method annotated with @Transactional calls a repository save method. If a runtime exception is thrown after the save call but before the method ends, what is the state of the database?
Spring Boot
@Service @Transactional public class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public void saveUserWithError(User user) { userRepository.save(user); if (true) { throw new RuntimeException("Error after save"); } } }
Attempts:
2 left
💡 Hint
Think about how @Transactional handles runtime exceptions and rollbacks.
✗ Incorrect
Spring rolls back the entire transaction if a runtime exception occurs, so the save is undone and no user is persisted.