When using @AssertTrue in methods, the method is invoked 4 times during validation (Bean Validation)

本秂侑毒 提交于 2019-12-10 17:52:35

问题


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?


回答1:


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:

@Test
public void testMotorcycle() throws Exception {
    AtomicInteger count = new AtomicInteger( 0 );

    Motorcycle motorcycle = new Motorcycle( count );
    validator.validate( motorcycle );
    assertEquals( motorcycle.count.get(), 1 );
}

class Motorcycle {
    final AtomicInteger count;
    private int fuel;
    private int tireDurability;

    Motorcycle(AtomicInteger count) {
        this.count = count;
    }

    @AssertTrue(message = "motorcycle.not.available.to.use")
    public boolean isAvailable() {
        count.incrementAndGet();
        return fuel > 0 && tireDurability > 0;
    }

}

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 ?



来源:https://stackoverflow.com/questions/45989381/when-using-asserttrue-in-methods-the-method-is-invoked-4-times-during-validati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!