Brilliant, it works. This is what I've done :
Code:
import org.hibernate.Hibernate;
import javax.validation.Path;
import javax.validation.TraversableResolver;
import java.lang.annotation.ElementType;
public class JPATraversableResolver implements TraversableResolver {
public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return traversableObject == null || Hibernate.isInitialized(traversableObject);
}
public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
return true;
}
}
Then I use my own JPATraversableResolver as follow :
Code:
TraversableResolver tr = new JPATraversableResolver();
Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
Country country = new Country(null, null);
Set<ConstraintViolation<Country>> violations = validator.validate(country);
assertEquals("Should have 2 constraints violation", 2, violations.size());
Good, now just need to code all that in an entity listener and trigger it at @PrePersist and @PreUpdate.
Thanks Emmanuel
Antonio