可以继承@Size hibernate注释吗?

时间:2011-10-16 17:02:24

标签: java hibernate validation bean-validation

我正在尝试使用hibernate验证来检查列表的大小。一般来说,我希望列表为空,除非它是B类型的对象。它似乎不起作用:

public static class A
{
    private List<Integer> inheritedRemoveAnnotation = new ArrayList<Integer>();

    @Size(max=0)
    public List<Integer> getInheritedRemoveAnnotation()
    {
        return inheritedRemoveAnnotation;
    }
}
public static class B extends A
{
    @Override
    @Size(max=Integer.MAX_VALUE)
    public List<Integer> getInheritedRemoveAnnotation()
    {
        return super.getInheritedRemoveAnnotation();
    }
}

private static Validator m_validator = Validation.buildDefaultValidatorFactory().getValidator();
public static void main(String[] args)
{
    B b = new B();
    b.getInheritedRemoveAnnotation().add(1);
    System.out.println(m_validator.validate(b));
}

我得到的输出是:
[ConstraintViolationImpl{interpolatedMessage='size must be between 0 and 0', propertyPath=inheritedRemoveAnnotation, rootBeanClass=class oshai.hibernatevalidators.TestInheritence$B, messageTemplate='{javax.validation.constraints.Size.message}'}]

1 个答案:

答案 0 :(得分:2)

还会检查层次结构中某处的所有约束。根据{{​​3}}:

  

如果重写方法,则会聚合约束注释。

相关问题