When using bean validation to validate the state of an object, the method annotated with @AssertTrue
is called 4 times whenever the validation is invoked. It should only be called once per invocation.
Hibernate-validator version: 5.1.3.Final
Here is an example:
For the following class Motorcycle:
import javax.validation.constraints.AssertTrue;
class Motorcycle{
private int fuel;
private int tireDurability;
@AssertTrue(message = "motorcycle.not.available.to.use")
public boolean isAvailable(){
return fuel > 0 && tireDurability > 0;
}
public void toUse(){...}
}
And the main:
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public class Main{
public static void main(String []args){
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Motorcycle>> violations = validator.validate(new Motorcycle());
}
}
When validator.validate(new Motorcycle())
is called, the method isAvailable()
is invoked 4 times.
Can anyone help me with this situation? Is this a bug? How do I work around it?
You should try using Hibernate Validator 6.0.2.Final (it's currently the latest available version). Next test does pass with this (latest) version of validator:
I did try to run it with 5.1 as well - and it also passes there. How did you determined that the method is called 4 times ?