Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.validation: Receive an error 'No validator could be found for type:'

Framework: JQuery, Spring, Hibernate, Hibernate Validator(JSR-303 Bean Validation)
Platform: Windows

I am trying to define a custom constraint @IdMustExist using JSR 303-Bean Validation. The purpose of the constraint is to check whether the entered id value exists in associated table. I am receiving an error 'javax.validation.UnexpectedTypeException: No validator could be found for type: com.mycompany.myapp.domain.package1.class1'.

If I place @IdMustExist on a field definition of Class1 (as given in example code below), I receive the above error. But If I place @IdMustExist constraint on a String field, I do not receive the above error. My code crashes in IdMustExistValidator.java. I dont understand why Hibernate can find Validator for 'String' class but not for domain class 'Class1'.

My class definitions are as follows.

Class2.java

@Entity
@Table
public class Class2 extends BaseEntity {
/**
 * Validation: Class1 Id must exist. 
 */ 
@ManyToOne
@JoinColumn(name="Class1Id")
@IdMustExist(entity=Class1.class)
private Class1 class1;

..

IdMustExist.java

@Required
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = IdMustExistValidator.class)
@Documented
@ReportAsSingleViolation
public @interface IdMustExist {
String message() default "{com.mycompany.myapp.domain.validation.constraints.idmustexist}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};

/**
 * The entity class that contains the id property.
 */
Class<?> entity();

/**
 * The property of the entity we want to validate for existence. Default value is "id"
 */
String idProperty() default "id";
}

IdMustExistValidator.java (Please note that this class design has bugs)

public class IdMustExistValidator implements ConstraintValidator<IdMustExist, Serializable> {

/**
 * Retrieve the entity class name and id property name
 */
public void initialize(IdMustExist idMustExist) {
    if (idMustExist.entity() != null) 
        entity = idMustExist.entity();
    idProperty = idMustExist.idProperty();
}

/**
 * Retrieve the entity for the given entity class and id property.
 * If the entity is available return true else false
 */
public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {
    logger.debug("Property Class = {}", property.getClass().getName());
    logger.debug("Property = {}", property);
    List resultList = commonDao.getEntityById(entity.getName(), idProperty);
    return resultList != null && resultList.size() > 0;
}

private Class<?> entity;
private String idProperty;

@Autowired
private CommonDao commonDao;
private final Logger logger = LoggerFactory.getLogger(IdMustExistValidator.class);

}

like image 755
Jayaprakash Avatar asked Jan 23 '11 16:01

Jayaprakash


1 Answers

Your constraint validator is declared to validate Serializable values. Perhaps Class2 is not Serializable?

like image 109
axtavt Avatar answered Sep 25 '22 21:09

axtavt