0

I have created a custom constraint and only want that to be called at pre-persist moment. Also, I have created the validation group and set it in the persistence.xml file according to the specification. It seems that the validation is being called at the right time (pre-persist), passing by my custom constraint implementation and forth, however I'm getting the exception below when the validation is accomplished.

Caused by: javax.validation.ConstraintViolationException: validation failed for classes [br.com.rcetecnologia.model.Orcamento] during persist time for groups [br.com.rcetecnologia.validator.annotations.ValidateOnPersistOnly, ]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:155)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:178)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:795)
at org.hibernate.search.jpa.impl.FullTextEntityManagerImpl.flush(FullTextEntityManagerImpl.java:157)

My persistence.xml file:

<persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
    ...
    <validation-mode>CALLBACK</validation-mode>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.search.default.directory_provider" value="filesystem"/> 
        <property name="hibernate.search.default.indexBase" value="/Users/gyowannyqueiroz/temp/indexes"/>

        <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.cache.use_query_cache" value="true"/>

        <property name="javax.persistence.validation.group.pre-persist" value="br.com.rcetecnologia.validator.annotations.ValidateOnPersistOnly" />
    </properties>
</persistence-unit>
  • 1
    Did you debug your validator? Constraint violation exception implies your validator was called but the validation did not pass. – Perception Apr 16 '12 at 14:37
  • Hi, I'm going to debug that to see if I can catch up something. I let you know the results. – Gyowanny Pessatto Queiroz Apr 16 '12 at 14:38
  • Ok, according to the debugger validation being performed [link](http://dl.dropbox.com/u/5587634/validation_successful.png), however I noticed that the Hibernate validator is throwing the exception. Why is that happening instead of having my jsf page displaying the validation message? Should I catch the ContraintValidationException where I'm calling the jpa's persist method? – Gyowanny Pessatto Queiroz Apr 16 '12 at 14:51
  • The display on your UI pages will depend on how you've configured JSF to handle error messages. Now, if your validation is succeeding but an error message is being thrown then thats a problem. Are you positive the validation actually succeeded? – Perception Apr 16 '12 at 15:05
  • Yes, since in the debugger I could see the validation message I configured in my validation messages bundle file. – Gyowanny Pessatto Queiroz Apr 16 '12 at 15:34
  • After debugging a little more, I noticed that the ConstraintViolationException is not being catch by the JSF framework. I have a custom exception handler where I've put a treatment for that exception in it. However the exception's stack trace is being printed before passing by the exception handler, by the org.hibernate.cfg.beanvalidation.BeanValidationEventListener. No clue. – Gyowanny Pessatto Queiroz Apr 16 '12 at 19:59
  • I surrounded my entitymanager.flush() with a try..catch block and the exception I'm getting is: java.lang.reflect.UndeclaredThrowableException. It should be the ConstraintViolationException, doesn't it? And why I can't catch the correct one? – Gyowanny Pessatto Queiroz Apr 16 '12 at 21:20

0 Answers0