2013-03-21 98 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,。這應該工作。