Search code examples
validationjakarta-eejpa-2.0eclipselinkglassfish-3

Bypass Bean Validation during a select but not in insert/update


I'm using Glassfish 3.1.1 (JSF 2, JPA 2, EclipseLink, Bean Validation).

I'm trying to implement validation with the Bean Validation API with these annotations: @Past, @Future and @NotNull.

It works fine during insert or update. Unfortunately, when I try to select en entity which have some incorrect values, I have this exception:

[#|2011-12-20T10:18:30.286+0100|WARNING|glassfish3.1.1|javax.enterprise.resource.jta.com.sun.enterprise.transaction|_ThreadID=36;_ThreadName=Thread-2;|DTX5014: Caught exception in beforeCompletion() callback:
javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.
    at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.validateOnCallbackEvent(BeanValidationListener.java:90)
    at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.preUpdate(BeanValidationListener.java:72)

I saw on the spec that I can disable JPA validation with this property javax.persistence.ValidationMode but how to disable validation during select but NOT during insert or update?

Thanks in advance for your help.


Solution

  • The issue is that anything in the persistence context will be validated.

    You could avoid adding the objects to the persistence context, by reading them in a different EntityManager outside of a transaction, or use the "eclipselink.read-only" hint on the query reading them.

    It does seem odd though that objects without changes are being validated. If this is the case, then perhaps log a bug.