JSR303 Pre-Persist / Pre-Persist / Pre-Remove Validation not covered by onConstraintViolations() and throws Exception

385 views
Skip to first unread message

Romain BIARD

unread,
Apr 3, 2012, 4:40:04 PM4/3/12
to google-we...@googlegroups.com

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 ?



Romain BIARD

unread,
Apr 3, 2012, 4:55:38 PM4/3/12
to google-we...@googlegroups.com
Maybe i have to override SimpleRequestProcessor.process(RequestMessage req, ResponseMessage resp)

around 

                // 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);


Patrick Julien

unread,
Apr 3, 2012, 5:31:51 PM4/3/12
to google-we...@googlegroups.com
Only the default validation group is supported GWT at this time.  I have no good solution for using another validation group because if you throw a validation exception from an handler, it triggers the wrong method of the Receiver<T> on the client side (onServerFailure instead of on constraint violation)
Reply all
Reply to author
Forward
0 new messages