gpt4 book ai didi

java - @Size hibernate注解可以继承吗?

转载 作者:行者123 更新时间:2023-11-30 04:58:18 24 4
gpt4 key购买 nike

我正在尝试使用 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='大小必须介于 0 和 0 之间', propertyPath=inheritedRemoveAnnotation, rootBeanClass=class oshai.hibernatevalidators.TestInheritence$B, messageTemplate='{javax.validation.constraints.Size.message}'}]

最佳答案

还会检查层次结构中某个位置的所有约束。根据documentation :

Constraint annotations are aggregated if methods are overridden.

关于java - @Size hibernate注解可以继承吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7785853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com