3 Replies Latest reply on Sep 20, 2010 2:12 AM by Emeric Kwemou

    Trouble Injecting DAO Into Hibernate Validator

    Schevus Osborne Newbie

      I am trying to insert a DAO into a custom validator to perform  unique constraint validation. Code is below:



      Field in my domain object



      @Required
          @Length(max = 45)
          @RestrictSpecialCharacters
          @UniqueAliasName
          public String getName() {
              return name;
          }




      Validator Interface



      @Documented
      @ValidatorClass(value = UniqueAliasNameValidator.class)
      @Target(value = {ElementType.METHOD})
      @Retention(value = RetentionPolicy.RUNTIME)
      public @interface UniqueAliasName {
      
          String message() default "Alias name is already in use.";
      }




      Validator Class



      public class UniqueAliasNameValidator implements Validator<UniqueAliasName> {
      
          private AliasDao aliasDao;
      
          public boolean isValid(Object value) {
              aliasDao = (AliasDao) Component.getInstance("aliasDao");
      
              boolean result = aliasDao.validateAliasNameUnique((String) value);
      
              return result;
          }
      
          public void initialize(UniqueAliasName uniqueAliasName) {}
      }




      validateAliasNameUnique Method



      public boolean validateAliasNameUnique(String aliasName) {
              Query query = entityManager.createNamedQuery("Alias.findByExactName");
              query.setParameter("aliasName", aliasName.toLowerCase());
      
              List aliasList = query.getResultList();
      
              return aliasList.isEmpty();
          }



      Originally my problem was injecting my DAO, but that seems to be working fine with the manual lookup. Everything seems to work fine until flush() is called on my EntityManager, at which point the process flow gets stuck in an infinite loop bouncing back and forth from my custom validator class to my DAO method, eventually resulting in a StackOverflowException.


      I realize this may be hibernate related (and I will post there also), but what I'm wondering is are there any repercussions for calling Component.getInstance()? Do I need to clear something out after doing that? I have seen at least a half dozen form posts with this problem with no clear resolution, so any help is appreciated. Thanks.