2012-07-23 44 views
0

我想在一個屬性上多次使用一個hiberante驗證器註釋。重複註釋 - 休眠驗證器

e.g:

@Length(min = 10, max = 10, groups = {Lenght10Group.class}) 
@Length(min = 5, max = 5, groups = {Lenght5Group.class}) 
private String productCode; 

我發現以下螺紋Multiple annotations of the same type on one element?,並試圖將其應用到我的應用程序。

我創建Lengths.java文件:

public @interface Lenghts { 
    Lenght[] value(); 
} 

,並以下列方式應用它:

@Lenghts({@Length(min = 10, max = 10, groups = {Lenght10Group.class}), 
      @Length(min = 5, max = 5, groups = {Lenght5Group.class}) }) 
private String productCode; 

,但它似乎並沒有工作。

我錯過了什麼?自定義驗證器是否需要,自定義處理程序?任何人都可以幫助我嗎?我會很感激。

回答

2

您應該使用Hibernate Validator開箱即用的@Length.List。所有內置約束(都由BV規範定義,由Hibernate Validator定義的附加自定義約束)定義了這樣一個@List註釋。

+0

聽起來不錯!我會明天嘗試,並告訴你是否有幫助。 – MrKiller21 2012-07-23 21:05:42

+0

Thx,問題解決了。 – MrKiller21 2012-07-24 07:39:51