Suppose I have code like this:
Vehicle vehicle = vehicleRepostory.findByIdInitialized(vehicleId);
If Vehicle cannot be found, this method will return null . The specification says that in this case I should throw a MyObjectNotFoundException so that the code becomes something like this:
Vehicle vehicle = vehicleRepostory.findByIdInitialized(vehicleId); MyObjectNotFoundException.throwIfNull(vehicle, Vehicle.class, vehicleId);
What if I want to get rid of calls to throwIfNull . In any case, this is not a DRY and bad design. There must be some kind of design template for me. I searched on the Internet, but it did not display anything useful.
A simple solution can put code in my repository, but I use SpringData for this, so this is just an interface:
public interface VehicleRepository extends Repository<Vehicle, Long> // ... @Query("select v from Vehicle v LEFT JOIN FETCH v.technicalData td LEFT JOIN FETCH v.registrationData rd" + " where v.vehicleId = ?") Vehicle findByIdInitialized(Long vehicleId);
source share