我剛用Hibernate實現了Bean驗證。由Sessionfactory.getCurrentSession.merge調用ConstraintValidator @Autowired bean null
如果我明確調用驗證程序,它將按預期工作,並且按預期注入連接到數據庫的@Autowired DAO bean。
我以前發現我需要在上面的工作之前添加下面的語句。之前我已經廣泛使用了@Autowired bean,但是下面的語句對於由Spring管理的驗證器以及注入到ConstraintValidator中的bean是必需的。
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
然而,當驗證是SessionFactory.getCurrentSession.merge時自動調用該bean爲空。
事實上,如果我直接用javax.Validation.validate調用調用驗證程序,它會起作用,這讓我認爲我已經正確設置了Spring配置。
我已經閱讀了一些人員無法獲得DAO bean @Autowired的帖子,但在我的情況下,它在合併期間被調用時除外。
下面的日誌輸出顯示驗證器直接被首先調用,然後作爲合併操作的結果被調用。
07.12.2011 01:58:13 INFO [http-8080-1] (FileTypeAndClassValidator:isValid) - Validating ...
07.12.2011 01:58:13 INFO [http-8080-1] (ConstraintValidatorHelper:getPropertyValue) - propertyName=className, returnValue=com.twoh.dto.PurchaseOrder
07.12.2011 01:58:13 INFO [http-8080-1] (ConstraintValidatorHelper:getPropertyValue) - propertyName=fileTypeId, returnValue=4
07.12.2011 01:58:13 INFO [http-8080-1] (QueryUtil:createHQLQuery) - select ft.id from FileType ft where ft.id = :fileTypeId and ft.fileClassName = :fileClassName
07.12.2011 01:58:13 INFO [http-8080-1] (BaseDAO:merge) - Entity: com.twoh.dto.PurchaseOrder: 1036.
07.12.2011 01:58:13 INFO [http-8080-1] (FileTypeAndClassValidator:isValid) - Validating ...
07.12.2011 01:58:13 INFO [http-8080-1] (ConstraintValidatorHelper:getPropertyValue) - propertyName=className, returnValue=com.twoh.dto.PurchaseOrder
07.12.2011 01:58:13 INFO [http-8080-1] (ConstraintValidatorHelper:getPropertyValue) - propertyName=fileTypeId, returnValue=4
07.12.2011 01:58:13 INFO [http-8080-1] (FileTypeAndClassValidator:isValid) - java.lang.NullPointerException
下面是對ConstraintValidator代碼:谷歌搜索後,我終於跨過這兩個說明它具有解決該問題的網站來了約18小時
package com.twoh.dto.ConstraintValidation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.twoh.dao.IQueryUtil;
@Component
public class FileTypeAndClassValidator implements ConstraintValidator<FileTypeAndClass, Object> {
private Log logger = LogFactory.getLog(this.getClass());
private String fileClassProperty;
private String fileTypeProperty;
@Autowired
private IQueryUtil queryUtil;
public void initialize(FileTypeAndClass constraintAnnotation) {
this.fileClassProperty = constraintAnnotation.fileClassProperty();
this.fileTypeProperty = constraintAnnotation.fileTypeProperty();
}
public boolean isValid(Object object, ConstraintValidatorContext constraintValidatorContext) {
boolean result = true;
logger.info("Validating ...");
if (object == null) {
result = false;
} else {
try {
String fileClassName = ConstraintValidatorHelper.getPropertyValue(String.class, fileClassProperty, object);
Integer fileTypeId = ConstraintValidatorHelper.getPropertyValue(Integer.class, fileTypeProperty, object);
result = queryUtil.createHQLQuery((
"select ft.id" +
" from FileType ft" +
" where ft.id = :fileTypeId" +
" and ft.fileClassName = :fileClassName"
))
.setParameter("fileTypeId", fileTypeId)
.setParameter("fileClassName", fileClassName)
.iterate().hasNext();
} catch (Exception e) {
logger.info(e);
}
}
return result;
}
}
不與Hibernate 4工作。無法在新版本中設置事件監聽器屬性。我也在嘗試一種基於集成商的方法。 stackoverflow.com/a/11672377/161628。但它報告重複事件監聽程序註冊錯誤。 –