views:

755

answers:

2

Hi all.

I have annotated a String field with @NotEmpty tag.

When I try to persist the entity with this filed's value "", it works fine. Mechanism detects the validation error and raises an InvalidStateException exception notifying the issue.

But when I try to persist the entity with this field's value null, an UndeclaredThrowableException (caused by InvocationTargetException, caused by PropertyValueException: not-null property references a null or transient value) exception is raised because system is trying to hit the database with null value on a non-nullable field.

The question is obvious: why validation mechanism does not detect this situation? I think it should raise an InvalidStateException exception instead of an UndeclaredThrowableException.

I was using the following libraries:

  - hibernate-annotations 3.3.1.GA
  - hibernate-validator 3.0.0.ga
  - hibernate-core 3.2.6ga

and now I just upgrade to

  - hibernate-annotations 3.4.0.GA
  - hibernate-validator 3.1.0.GA
  - hibernate-core 3.3.2

These are the latest stable releases.

But it still not working.

Any idea?

Full error trace:

java.lang.reflect.UndeclaredThrowableException
    at $Proxy78.merge(Unknown Source)
    at com.myApp.persistence.ddg.impl.MergeMethodInterceptor.intercept(MergeMethodInterceptor.java:18)
    at com.myApp.backoffice.model.cms.dao.IDAOOrganization$$EnhancerByCGLIB$$c21eebfe.merge(<generated>)
    at com.myApp.cms.components.implementations.DSOrganization.saveOrUpdate(DSOrganization.java:150)
    at com.myApp.cms.components.implementations.DSOrganization.saveOrUpdate(DSOrganization.java:1)
    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:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at com.myApp.persistence.tx.impl.TransactionalInterceptor$1.call(TransactionalInterceptor.java:34)
    at com.myApp.persistence.tx.impl.TransactionManagerImpl.createSessionAndTransactionAndRunCallable(TransactionManagerImpl.java:261)
    at com.myApp.persistence.tx.impl.TransactionManagerImpl.runInTransaction(TransactionManagerImpl.java:236)
    at com.myApp.persistence.tx.impl.TransactionalInterceptor.invoke(TransactionalInterceptor.java:27)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy70.saveOrUpdate(Unknown Source)
    at com.myApp.cms.components.ValidationTest.test_organization(ValidationTest.java:60)
    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:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    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)
Caused by: java.lang.reflect.InvocationTargetException
    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:597)
    at com.myApp.persistence.tx.impl.TransactionManagerImpl$TimeoutInvocationHandler.invoke(TransactionManagerImpl.java:176)
    ... 44 more
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.myApp.backoffice.model.cms.dto.Organization.name
    at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:290)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:186)
    at org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener.entityIsTransient(IdTransferringMergeEventListener.java:58)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:240)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
    at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
    ... 49 more

Trying to register the merge event listener to the sessionFactory bean:

<property name="eventListeners">
  <map>
    <entry key="merge">
      <bean class="org.hibernate.validator.event.ValidateEventListener"/>
    </entry>
  </map>
</property>

It raises the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.myApp.persistence.tx.TransactionalAutoProxyCreator#0' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Cannot resolve reference to bean 'transactionalInterceptor' while setting bean property 'transactionalInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionalInterceptor' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Invocation of init method failed; nested exception is java.lang.ArrayStoreException: org.hibernate.validator.event.ValidateEventListener
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:881)
        at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:597)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:366)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
        at com.myApp.ContextLoader.getApplicationContext(ContextLoader.java:27)
        at com.myApp.ContextLoader.getBean(ContextLoader.java:43)
        at com.myApp.cms.CMSTestContext.getIDSPerson(CMSTestContext.java:200)
        at com.myApp.cms.CMSTestContext.removePersons(CMSTestContext.java:486)
        at com.myApp.cms.CMSTestContext.removeEditorialObjs(CMSTestContext.java:376)
        at com.myApp.cms.CMSTestContext.removeCollections(CMSTestContext.java:323)
        at com.myApp.cms.CMSTestContext.after(CMSTestContext.java:1572)
        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:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:37)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
        at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
        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)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionalInterceptor' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Invocation of init method failed; nested exception is java.lang.ArrayStoreException: org.hibernate.validator.event.ValidateEventListener
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 46 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Invocation of init method failed; nested exception is java.lang.ArrayStoreException: org.hibernate.validator.event.ValidateEventListener
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 59 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/myApp/backoffice/model/persistence.xml]: Invocation of init method failed; nested exception is java.lang.ArrayStoreException: org.hibernate.validator.event.ValidateEventListener
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 72 more
    Caused by: java.lang.ArrayStoreException: org.hibernate.validator.event.ValidateEventListener
        at org.hibernate.cfg.Configuration.setListener(Configuration.java:1694)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:721)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
        ... 82 more

Thanks in advance.

+1  A: 

First of all, "merge before save" doesn't make sense. You can use merge() instead of save() when appropriate, but merge() already returns a persistent instance - the following save() is pointless.

Secondly, merge() does not invoke pre-insert / pre-update event listeners by default as far as I know; thus validation never gets performed. You can work around this by manually specifying listener for merge event or manually invoking validatior prior to merging.

ChssPly76
But with empty ("") value it works!!! I mean, validator detects the validation fail, even when call merge before save.
Alberthoven
With empty string merge() saves your entity successfully (empty string is not null - so there's no Nullability exception). The following save() invokes the validator which fails due to @NotEmpty; the entire transaction is then rolled back.
ChssPly76
How could I force to validate before merging? there is no event for merge in ValidateEventListener array
Alberthoven
What array? I've posted a link in my answer above - you need to add a new `<event>` entry to your Hibernate configuration with event type being "merge". I would not necessarily recommend this, though - merge semantics is rather tricky. Why are you using it instead of, say, saveOrUpdate() in the first place?
ChssPly76
My saveOrUpdate needs to call merge due to possible scenarios (...)Like I'm using annotations, I have no hibernate.cfg.xml file, but persistence.xml, where I define the following beans:- sessionFactory( org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean)- dataSource- transactionManagerand many interceptors....¿Where could I add my "merge" event? ¿which bean could accept <event type="merge"> <listener class="org.hibernate.validator.event.ValidateEventListener"/> </event>?
Alberthoven
AnnotationSessionFactoryBean has an `eventListeners` parameter which is a map of listener classes keyed by event types. So, `<property name="eventListeners"><map><entry key="merge" value="org.hibernate.validator.event.ValidateEventListener"/></map></property>`. Check Spring documentation (Hibernate integration) for specific details.
ChssPly76
Ummm, that is just what I did when I tried to register the merge listener... but I got an error loading the context:Caused by: java.lang.ArrayStoreException: org.hibernate.validator.event.ValidateEventListenerI have added the full trace to the question's body.
Alberthoven
Hmm... It seems the problem is `ValidateEventListener` does not implement `MergeEventListener` interface unlike, say, `PreInsertEventListener` so Hibernate can't assign it to `merge` event. You'll need to extend it, implement `MergeEventListener` and call through to `validate()` from `onMerge()`. Take a look at `ValidationEventListener.onPreInsert()` for an example. This goes back to "tricky merge event semantics"; perhaps you should consider invoking validator manually instead (check the link in my answer for details)
ChssPly76
Ok, I will invoke the validation manually. Thanks!
Alberthoven
A: 

I got the same error which fixed itself once i did the following:

-public class CustomEventListener implements PreUpdateEventListener{...}

( instead of the earlier code which was: -public class CustomEventListener{..} )

scout