Complete the code to declare a service class in Spring Boot.
@[1] public class UserService { // business logic here }
The @Service annotation marks the class as a service component in Spring Boot, which holds business logic.
Complete the code to inject a repository into the service using Spring Boot.
@Service public class OrderService { private final OrderRepository orderRepository; public OrderService([1] orderRepository) { this.orderRepository = orderRepository; } }
The constructor parameter must be of type OrderRepository to inject the repository instance.
Fix the error in the method that saves an order in the service.
public void saveOrder(Order order) {
orderRepository.[1](order);
}The correct method to save an entity in Spring Data JPA repository is save.
Fill both blanks to create a method that finds an order by ID and returns it or null if not found.
public Order getOrderById(Long id) {
return orderRepository.[1](id).[2](null);
}findById returns an Optional. Using orElse(null) returns the order or null if not found.
Fill all three blanks to create a method that updates an order's status if it exists.
public boolean updateOrderStatus(Long id, String status) {
return orderRepository.findById([1]).map(order -> {
order.setStatus([2]);
orderRepository.[3](order);
return true;
}).orElse(false);
}The method finds the order by id, sets the new status, saves the order with save, and returns true if successful.