title: @Autowired bean null in ConstraintValidator when invoked by Sessionfactory.getCurrentSession.merge
body: <p>I have just implemented Bean Validation with Hibernate.</p>
  
  <p>If I call the validator explicitly it works as expected and my @Autowired DAO bean that connects to the DB is injected as expected.</p>
  
  <p>I had previously discovered that I needed to add the statement below before the above would work. I had made extensive use of @Autowired beans before but the statement below was necessary for the validator to be managed by Spring and the bean injected into the ConstraintValidator.</p>
  
  <pre><code>&lt;bean id="validator"
  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt;
  </code></pre>
  
  <p>However when the validator is called automatically during a SessionFactory.getCurrentSession.merge the bean is null.</p>
  
  <p>The fact that it works if I invoke the validator directly with a call to javax.Validation.validate makes me think that I have set up the Spring configuration correctly.</p>
  
  <p>I have read a number for posts where people have been unable to get the DAO bean @Autowired but in my case it is except when called during the merge.</p>
  
  <p>The log output below shows the validator being called directly first and then being called as as a result of a merge operation.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Below is the code for the ConstraintValidator:</p>
  
  <pre><code>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&lt;FileTypeAndClass, Object&gt; {
  
      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;
      }
  }
  </code></pre>