On a rest ubdate request , first i am reading related object from database ,second mapping dto to just readed dbObject using dozer framework standart mapper,DozerBeanMapper . Next i am trying to validate new mapped object but validation fail because of one field annotated with
@NotEmpty(message = "Name cannot be empty ")
is seems null ,i know this occurs because dbObject is a dynamic proxy object refering my entity handled by hibernate ,is there any way validating managed bean annotated with hibernate.validator.constaints using hibernate validator .
@PUT
public Response update(QueryDTO dto) throws ServiceException {
log.info("Request for upadating query");
Query dbObject= this.persistenceService.find( Query.class,dto.getSystemId());
if(dbObject.getVersion()!=dto.getVersion()){
throw new InvalidVersionException("Db version is" + dbObject.getVersion());
}
this.mapper.map(dto,dbObject);
Set<ConstraintViolation<Query>> validationResult= this.validator.validate(dbObject);
if(!validationResult.isEmpty()){
throw new ValidationException(StringifyUtil.buildExceptionMessage(validationResult));
}
Query updatedObject= this.persistenceService.update(dbObject);
return ok(updatedObject);
}
In the BeanValidation API a TraversableResolver is used to check if a property is reachable by the validator. Depending on the hibernate validator version you are using there are implementations for Hibernate/JPA addressing this problem.
see http://docs.oracle.com/javaee/7/api/javax/validation/TraversableResolver.html https://docs.jboss.org/hibernate/validator/5.0/api/org/hibernate/validator/internal/engine/resolver/package-summary.html
You can configure a TraversableResolver this way
final Configuration<?> conf = Validation.byDefaultProvider().configure();
conf.traversableResolver(new MyTraversableResolver());
ValidatorFactory validatorFactory = conf.buildValidatorFactory();
see https://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-bootstrapping.html#d0e4238 for bootstrapping the validator