【问题标题】:Validation class hierarchy override验证类层次结构覆盖
【发布时间】:2013-03-21 17:23:53
【问题描述】:

是否可以覆盖类层次结构以进行 hibernate/javax 验证? 或者如何通过扩展类来覆盖参数验证?

使用@Valid 注释并在我的类中设置javax 验证注释。我想要一个扩展类,其中删除了验证。 我发现休眠验证循环遍历整个类层次结构,甚至在超类中检查验证。 我从使用 Spring MVC 设置的基于 Json 的 REST API 中使用它。

例子:

public class Parent {
    protected Integer numChildren;

    @NotNull(message = "NumChildren has to be set.") 
    @Size(min 1, max 10, message = "A partent has to have at least one kid.")
    public Integer getNumChildren() {
        return numChildren;
    }

    public void setNumChildren(Integer numChilds) {
        this.numChildren = numChilds;
    }
}

public class Child extends Parent {

    // A child doesnt have to have kids, but can, so I want to override this parameter
    // and remove the validation requirement, and make it just optional.
    @Override
    public Integer getNumChildren() {
        return super.numChildren;
    }
}

最好的问候, 马库斯

【问题讨论】:

    标签: java hibernate validation hibernate-validator


    【解决方案1】:

    Bean Validation 不允许禁用/更改超类中的约束。约束总是结合在一起的。我猜要走的路是通过组,例如ParentChecks 组和ChildChecks 组。将特定于孩子的父母的约束分配给相应的组。然后,您可以将父级的默认组重新定义为 ParentChecks,_Parent_,将子级的默认组重新定义为 ChildChecks, Child。应该可以的。

    【讨论】:

      猜你喜欢
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多