org.hibernate.validator.InvalidStateException: validation failed for : my.package.A

Hibernate JIRA | Julien Kronegg | 8 years ago
  1. 0

    According to Hibernate validation documentation ( http://www.hibernate.org/hib_docs/validator/reference/en/html_single/#validator-checkconstraints-orm-jpaevent ) we can annotate an entity with @EntityListeners({JPAValidateListener.class}) to validate the fields using the @PrePersist / @PreUpdate JPA events handlers. I tryied with the following entity: @Entity @Table(...) @EntityListeners({JPAValidateListener.class}) public class A { private int id; private String text; @Id @Column(...) @NotNull @GeneratedValue(...) @GenericGenerator(...) public int getId() { return id; } public void setId(int id) { this.id=id; } ... } Problem is that the @PrePersist event is raised before some fields are automatically filled (e.g. @Id annotated). So the JPAValidateListener raises an exception saying that the identifier cannot be null. The exception root cause detail is (InvalidState: id cannot be null) and the stacktrace is: org.hibernate.validator.InvalidStateException: validation failed for : my.package.A at org.hibernate.validator.ClassValidator.assertValid(ClassValidator.java:666) at org.hibernate.validator.event.JPAValidateListener.onChange(JPAValidateListener.java:62) ...// some reflection StacktraceElement at org.hibernate.ejb.event.ListenerCallback.invoke(ListenerCallback.java:31) at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:80) at org.hibernate.ejb.event.EntityCallbackHandler.preCreate(EntityCallbackHandler.java:49) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:48) at org.hibernate.event.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154) at org.hibernate.event.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110) at org.hibernate.event.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61) at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623) ... The EJB3PersistEventListener.saveWithGeneratedId() method looks like: protected Serializable saveWithGeneratedId(...) { callbackHandler.preCreate(entity); // raises the @PrePersist JPA event return super.saveWithGeneratedId(...); // sets the identifier } Thus, the JPAValidateListener validates the whole Entity and does not take the entity lifecycle into account. The JPAValidateListener should check all fields except the ones which are known to be modified/setled after the call to the validator. This may also be the case for other generated fields such as the ones annotated with @Version (I did not check that).

    Hibernate JIRA | 8 years ago | Julien Kronegg
    org.hibernate.validator.InvalidStateException: validation failed for : my.package.A
  2. 0

    According to Hibernate validation documentation ( http://www.hibernate.org/hib_docs/validator/reference/en/html_single/#validator-checkconstraints-orm-jpaevent ) we can annotate an entity with @EntityListeners({JPAValidateListener.class}) to validate the fields using the @PrePersist / @PreUpdate JPA events handlers. I tryied with the following entity: @Entity @Table(...) @EntityListeners({JPAValidateListener.class}) public class A { private int id; private String text; @Id @Column(...) @NotNull @GeneratedValue(...) @GenericGenerator(...) public int getId() { return id; } public void setId(int id) { this.id=id; } ... } Problem is that the @PrePersist event is raised before some fields are automatically filled (e.g. @Id annotated). So the JPAValidateListener raises an exception saying that the identifier cannot be null. The exception root cause detail is (InvalidState: id cannot be null) and the stacktrace is: org.hibernate.validator.InvalidStateException: validation failed for : my.package.A at org.hibernate.validator.ClassValidator.assertValid(ClassValidator.java:666) at org.hibernate.validator.event.JPAValidateListener.onChange(JPAValidateListener.java:62) ...// some reflection StacktraceElement at org.hibernate.ejb.event.ListenerCallback.invoke(ListenerCallback.java:31) at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:80) at org.hibernate.ejb.event.EntityCallbackHandler.preCreate(EntityCallbackHandler.java:49) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:48) at org.hibernate.event.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154) at org.hibernate.event.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110) at org.hibernate.event.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61) at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623) ... The EJB3PersistEventListener.saveWithGeneratedId() method looks like: protected Serializable saveWithGeneratedId(...) { callbackHandler.preCreate(entity); // raises the @PrePersist JPA event return super.saveWithGeneratedId(...); // sets the identifier } Thus, the JPAValidateListener validates the whole Entity and does not take the entity lifecycle into account. The JPAValidateListener should check all fields except the ones which are known to be modified/setled after the call to the validator. This may also be the case for other generated fields such as the ones annotated with @Version (I did not check that).

    Hibernate JIRA | 8 years ago | Julien Kronegg
    org.hibernate.validator.InvalidStateException: validation failed for : my.package.A
  3. 0

    My Customer class has two address (embedded), one must be validated, the other dont. When the address2 is null, the validation works. But when the empty instance is setted, the persist crashes. Validating by ClassValidator works fine. But when I call the em.persist() in service, the exception is throwed: javax.ejb.EJBException: nested exception is: org.hibernate.validator.InvalidStateException: validation failed for: br.com.kinomaxx.kinosystem.Customer org.hibernate.validator.InvalidStateException: validation failed for: br.com.kinomaxx.kinosystem.Customer at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:148) at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172) at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:119) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:42) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49) at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131) at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87) at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38) at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592) at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596) at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:213) at br.com.kinomaxx.kinosystem.dao.EntityManagerWrapperBean.persist(EntityManagerWrapperBean.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:45) at org.mycontainer.jpa.PersistenceContextInterceptor.intercept(PersistenceContextInterceptor.java:53) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.kernel.interceptors.NamingInjectInterceptor.intercept(NamingInjectInterceptor.java:48) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.kernel.interceptors.NamingInjectInterceptor.intercept(NamingInjectInterceptor.java:48) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.ejb.interceptor.EJBContextInterceptor.intercept(EJBContextInterceptor.java:55) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.ejb.transaction.RequiredTransactionHandler.intercept(RequiredTransactionHandler.java:38) at org.mycontainer.ejb.interceptor.TransactionInterceptor.intercept(TransactionInterceptor.java:52) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.ejb.interceptor.EJBExceptionInterceptor.intercept(EJBExceptionInterceptor.java:27) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.kernel.reflect.proxy.ProxyEngine.invoke(ProxyEngine.java:77) at $Proxy33.persist(Unknown Source) at br.com.kinomaxx.kinosystem.CustomerTest.test(CustomerTest.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) javax.ejb.EJBException: nested exception is: org.hibernate.validator.InvalidStateException: validation failed for: br.com.kinomaxx.kinosystem.Customer at org.mycontainer.ejb.interceptor.EJBExceptionInterceptor.intercept(EJBExceptionInterceptor.java:31) at org.mycontainer.kernel.reflect.proxy.ProxyChainImpl.proceed(ProxyChainImpl.java:35) at org.mycontainer.kernel.reflect.proxy.ProxyEngine.invoke(ProxyEngine.java:77) at $Proxy33.persist(Unknown Source) at br.com.kinomaxx.kinosystem.CustomerTest.test(CustomerTest.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

    Hibernate JIRA | 9 years ago | Raoni Fortes Normanton
    org.hibernate.validator.InvalidStateException: validation failed for: br.com.kinomaxx.kinosystem.Customer
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    2007-06-27 21:18:39,133 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 11829719154 (SessionImpl.java:220) 2007-06-27 21:18:39,159 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.DefaultSaveOrUpdateEventListener - saving transient instance (DefaultSaveOrUpdateEventListener.java:158) 2007-06-27 21:18:39,162 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.event.def.AbstractSaveEventListener - generated identifier: e2632934-a800-4a91-a9ff-ce8e1f95a03d, using strategy: org.hibernate.id.Assigned (AbstractSaveE ventListener.java:112) 2007-06-27 21:18:39,168 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.AbstractSaveEventListener - saving [mypackage.server.types.Fee#e2632934-a800-4a91-a9ff-ce8e1f95a03d] (AbstractSaveEventListener.java: 153) 2007-06-27 21:18:39,228 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.AbstractFlushingEventListener - flushing session (AbstractFlushingEventListener.java:58) 2007-06-27 21:18:39,232 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades (AbstractFlushingEventListener.java:111) 2007-06-27 21:18:39,237 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections (AbstractFlushingEventListener.java:154) 2007-06-27 21:18:39,238 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections (AbstractFlushingEventListener.java:171) 2007-06-27 21:18:39,249 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.AbstractFlushingEventListener - Processing unreferenced collections (AbstractFlushingEventListener.java:210) 2007-06-27 21:18:39,251 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates (AbstractFlushingEventListener.java:224) 2007-06-27 21:18:39,252 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects (AbstractFlushingEventListener.java:85) 2007-06-27 21:18:39,254 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections (AbstractFlushingEventListener.java:91) 2007-06-27 21:18:39,260 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.pretty.Printer - listing entities: (Printer.java:83) 2007-06-27 21:18:39,262 [WorkManager.DefaultWorkManager : 0] DEBUG org.hibernate.pretty.Printer - mypackage.types.Fee{<values anonymized>} (Printer.java:90) 2007-06-27 21:18:39,264 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.event.def.AbstractFlushingEventListener - executing flush (AbstractFlushingEventListener.java:290) 2007-06-27 21:18:39,265 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.jdbc.ConnectionManager - registering flush begin (ConnectionManager.java:469) 2007-06-27 21:18:39,276 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.jdbc.ConnectionManager - registering flush end (ConnectionManager.java:478) 2007-06-27 21:18:39,278 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.impl.SessionImpl - after transaction completion (SessionImpl.java:422) 2007-06-27 21:18:39,280 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.impl.SessionImpl - closing session (SessionImpl.java:273) 2007-06-27 21:18:39,281 [WorkManager.DefaultWorkManager : 0] TRACE org.hibernate.jdbc.ConnectionManager - connection already null in cleanup : no action (ConnectionManager.java:375) 2007-06-27 21:18:39,298 [WorkManager.DefaultWorkManager : 0] ERROR Could not insert Fee org.hibernate.validator.InvalidStateException: validation failed for: mypackage.server.types.Fee at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:148) at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172) at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:156) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:49) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141) This only seems to happen when I'm running inside an appserver (WebSphere 6.1) with pooled connections - in my unit-tests using a org.springframework.jdbc.datasource.DriverManagerDataSource (eg. non-pooled) it works OK - and only if adding validating annotations (@NotEmpty)

    Hibernate JIRA | 10 years ago | David J. M. Karlsen
    org.hibernate.validator.InvalidStateException: validation failed for: mypackage.server.types.Fee
  6. 0

    Validation Exception caused by @Length and @Enu... | JBoss Developer

    jboss.org | 1 year ago
    org.hibernate.validator.InvalidStateException: validation failed for: com.paragon.spurs.model.Project

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. org.hibernate.validator.InvalidStateException

      validation failed for : my.package.A

      at org.hibernate.validator.ClassValidator.assertValid()
    2. Hibernate Validator Engine
      ClassValidator.assertValid
      1. org.hibernate.validator.ClassValidator.assertValid(ClassValidator.java:666)
      1 frame
    3. org.hibernate.validator
      JPAValidateListener.onChange
      1. org.hibernate.validator.event.JPAValidateListener.onChange(JPAValidateListener.java:62)
      1 frame