2013-03-21 101 views
0

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

使用@Valid注释并在我的类中设置了javax验证注释。我想有一个扩展类,其中验证被删除。 我发现hibernate验证循环遍历整个类层次结构,甚至在超类中检查验证。 我从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; 
    } 
} 

此致 马库斯

回答

0

Bean验证不允许禁用超级类/变化约束。约束始终合并。我想要走的路将通过组,例如ParentChecks组和ChildChecks组。将特定于子项的父项的约束分配给相应的组。然后,您可以重新定义父母的默认组为ParentChecks,_Parent_和子代为ChildChecks,。这应该工作。