【发布时间】:2011-12-07 10:04:14
【问题描述】:
我刚刚使用 Hibernate 实现了 Bean 验证。
如果我显式调用验证器,它会按预期工作,并且连接到数据库的 @Autowired DAO bean 会按预期注入。
我之前发现我需要在上面添加下面的语句才能起作用。我之前已经广泛使用了@Autowired bean,但是下面的语句对于验证器由 Spring 管理并且 bean 注入到 ConstraintValidator 中是必要的。
<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 的代码:
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 spring bean-validation autowired