I am trying to insert a DAO into a custom validator to perform unique constraint validation. Code is below:
Field in my domain Object
Code:
@Required
@Length(max = 45)
@RestrictSpecialCharacters
@UniqueAliasName
public String getName() {
return name;
}
Validator InterfaceCode:
@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 ClassCode:
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) {}
}
DAO MethodCode:
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 have read through several forum posts about problems identical or similar to this, but the best resolution I have found was:
"Calls to BusinessProcsessContext.flush() was causing an infinite recursion when additions or removals are not empty."
I'm not sure what this means, so that is of little help. Any help or information is appreciated.