2012-07-25 51 views
0

接縫2時,JBoss 6.1SEAM與Hibernate驗證

我目前我的持久化對象使用Hibernate驗證註釋與縫做我的JSF頁面的驗證:

@Entity 
@Table(name = "CLIENT", catalog = "testdb") 
public class Client implements java.io.Serializable { 

private Integer    id; 
private String    name; 

@Id 
@GeneratedValue(strategy = IDENTITY) 
@Column(name = "ID", unique = true, nullable = false) 
public Integer getId() { 
    return this.id; 
} 

@Column(name = "NAME", unique = true, nullable = false, length = 64) 
@org.hibernate.validator.Length(min=4,max=64) 
public String getName() { 
    return this.name; 
} 

...Setters and rest 
} 

這工作完全當名字段長度小於4個字符時,會顯示一個很好的錯誤。

的問題是用戶能夠在其它渠道提交的數據,即電子表格導入,三維超聲心動圖的第三方應用程序,等等。因此我必須做確認我的java代碼如下:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); 
Validator validator = factory.getValidator(); 
Set<ConstraintViolation<K>> constraintViolations = validator.validate(bean); 

現在,這並不挑發現任何驗證錯誤,但堅持數據確實會拋出異常,因此它可以在某個地方工作。我可以通過使用@ org.hibernate.validator.constraints.Length來實現它,但是然後jsf屏幕不報告約束問題。

我在我看來像@ org.hibernate.validator.Length是舊的遺留Hibernate驗證程序庫,但這是非常糾結到接縫2,因此無法刪除。

任何人都知道如何使用java代碼中的遺留hibernate註釋進行驗證。

回答

1

無需手動調用hibernate驗證。保存處理錯誤

try { 
     save(); 
    } catch (InvalidStateException e) { 
     logHibernateValidationException(e); 
     ... 

private void logHibernateValidationException(InvalidStateException e) { 
    for (InvalidValue invalidValue : e.getInvalidValues()) { 
     LOGGER.warn("Validation Failed. Instance of bean class: {0} has an invalid property: {1} with message: {2}", 
       invalidValue.getBeanClass().getSimpleName(), 
       invalidValue.getPropertyName(), 
       invalidValue.getMessage()); 
    } 
}