In project i’m currently working on, we use a lot of java bean validations. In many of these validations we need to retrieve entities from data layer so they can be used during validation process.
Let’s say for example we have class Car.java
@FirstValidation
@SecondValidation
public class Car {
private Engine engine;
private List<Tires> tires;
...
}
@FirstValidation
constraint code looks like:
public class FirstValidationValidator implements ConstraintValidator<FirstValidation, Car> {
private final EngineRepository engineRepository;
@Override
public boolean isValid(Car car, ConstraintValidatorContext context) {
Engine engine = engineRepository.findEngineByCarId(car.getId());
// do some validation with engine instance
}
}
@SecondValidation
constraing code looks like:
public class SecondValidationValidator implements ConstraintValidator<SecondValidation, Car> {
private final EngineRepository engineRepository;
@Override
public boolean isValid(Car car, ConstraintValidatorContext context) {
Engine engine = engineRepository.findEngineByCarId(car.getId());
// do some validation with engine instance
}
}
What i’m interested is there a way i can find entities used in validations before validations start and put them in some kind of context, so i don’t have to extract these entities for every validation separately?