I just implemented Bean Validation with Hibernate.
If I call the validator explicitly, it works as expected, and my @Autowired DAO Bean, which connects to the database, is entered as expected.
Earlier, I discovered that I needed to add instructions below before working on it would work. I used @Autowired beans extensively, but the following instruction was necessary to control the validator using Spring and Bean introduced in ConstraintValidator.
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
However, when the validator is called automatically during SessionFactory.getCurrentSession.merge, the Bean is null.
The fact that it works, if I call the validator directly with javax.Validation.validate, makes me think that I configured Spring correctly.
I read the number for messages in which people were unable to get the DAO Bean @Autowired, but in my case this happens, except when called during a merge.
The output below shows that the validator is called directly first, and then called as a result of the merge operation.
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
The following is the code for 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; } }