Hi all,
I'm writing Custom Validators (server side I mean) in order to validate an entity before a persist() call (and only in this specific case).
Actually my final goal is to display localized messages when users want to insert a duplicate entry in my CRUD Webapp.
To do this, i have tried to play with validation groups and validation triggers. In Hibernate you can associate Custom ValidationGroup (Here com.test.cis.server.validator.PreRemove , com.test.cis.server.validator.PrePersist, com.test.cis.server.validator.PreUpdate) and validation triggers.
persistence.xml
<property name="javax.persistence.validation.group.pre-remove" value="com.test.cis.server.validator.PreRemove"/>
<property name="javax.persistence.validation.group.pre-persist" value="javax.validation.groups.Default,com.test.cis.server.validator.PrePersist"/>
<property name="javax.persistence.validation.group.pre-update" value="javax.validation.groups.Default,com.test.cis.server.validator.PreUpdate"/>
On my bean, I've put my custom annotation with com.test.cis.server.validator.PrePersist.class as validation group.
@MyConstraint(groups=com.test.cis.server.validator.PrePersist.class)
So my problem turns around the GWT validation with requestfactory : the validation is done before (so the validation will not execute my custom validator at this time ) the call to persist (which will trigger myConstraint validator) so GWT can't handle Constraint Violation exception that occurs during a method call(i.e during method execution). In this case, the exception is catch by the standard ExceptionHandling mechanism and every informations / Localized messages are lost.
javax.validation.ConstraintViolationException: validation failed for classes [com.test.domain.entity.CatalogueEntity] during persist time for groups [javax.validation.groups.Default, com.test.cis.server.validator.PrePersist, ]
Have you ever been in this case ? Another solution ?
// Validate entities
List<ViolationMessage> errorMessages = validateEntities(source);
if (!errorMessages.isEmpty()) {
resp.setViolations(errorMessages);
return;
}
RequestState returnState = new RequestState(source);
// Invoke methods
List<Splittable> invocationResults = new ArrayList<Splittable>();
List<Boolean> invocationSuccess = new ArrayList<Boolean>();
processInvocationMessages(source, req, invocationResults, invocationSuccess, returnState);