-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: @NotEmpty does not work for null values
PostPosted: Mon Aug 03, 2009 4:28 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
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.

Any idea?

Thanks in advance.

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Wed Aug 05, 2009 10:53 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

which version of Hibernate Validator are you using and how does your annotated entity look like? Also, could you post the full stack trace?

--Hardy


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Wed Aug 05, 2009 12:53 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
The libraries versions I'm using are:
    - hibernate-annotations 3.3.1.GA
    - hibernate-validator 3.0.0.ga

Following I include the relevant part of the POJO:
Code:
@Entity
@Table(name = "org_organization", schema = "cms")
public class Organization implements Serializable {

   // Other fields...

   private String name = null;

   // Other getters/setters

   @Column(name = "org_name")
   @NotEmpty
   @Length(max = ModelUtils.NAME_LENGTH, message = "max length allowed = " + ModelUtils.NAME_LENGTH)
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}


Case 1:
Code:
Organization organization = new Organization();
organization.setName("");
// persist the entity invoking saveOrUpdate()


Result 1:
Code:
org.hibernate.validator.InvalidStateException: validation failed for: com.dixired.backoffice.model.cms.dto.Organization
   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:160)
   at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:53)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at com.dixired.persistence.tx.impl.TransactionManagerImpl.createSessionAndTransactionAndRunCallable(TransactionManagerImpl.java:218)
   at com.dixired.persistence.tx.impl.TransactionManagerImpl.runInTransaction(TransactionManagerImpl.java:188)
   at com.dixired.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 $Proxy79.saveOrUpdate(Unknown Source)
   at com.dixired.cms.components.ErrorTest.test_organization(ErrorTest.java:56)
   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)


With the InvalidStateException object I get all data related with the problem with the message "name : no debe ser nulo o vacío".
That is OK.

Case 2:
Code:
Organization organization = new Organization();
organization.setName(null);
// persist the entity invoking saveOrUpdate()


Result 2:
Code:
java.lang.reflect.UndeclaredThrowableException
   at $Proxy87.saveOrUpdate(Unknown Source)
   at com.dixired.persistence.ddg.impl.SaveOrUpdateMethodInterceptor.intercept(SaveOrUpdateMethodInterceptor.java:18)
   at com.dixired.backoffice.model.cms.dao.IDAOOrganization$$EnhancerByCGLIB$$2dcbb95.saveOrUpdate(<generated>)
   at com.dixired.cms.components.implementations.DSOrganization.saveOrUpdate(DSOrganization.java:152)
   at com.dixired.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.dixired.persistence.tx.impl.TransactionalInterceptor$1.call(TransactionalInterceptor.java:34)
   at com.dixired.persistence.tx.impl.TransactionManagerImpl.createSessionAndTransactionAndRunCallable(TransactionManagerImpl.java:216)
   at com.dixired.persistence.tx.impl.TransactionManagerImpl.runInTransaction(TransactionManagerImpl.java:188)
   at com.dixired.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 $Proxy79.saveOrUpdate(Unknown Source)
   at com.dixired.cms.components.ErrorTest.test_organization(ErrorTest.java:56)
   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.dixired.persistence.tx.impl.TransactionManagerImpl$TimeoutInvocationHandler.invoke(TransactionManagerImpl.java:129)
   ... 44 more
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.dixired.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.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
   ... 49 more


An UndeclaredThrowableException is raised instead of an InvalidStateException. So I can't access to error data properly. ¿Why validation mechanism does not detect the null value before checkNullability is invoked?

Thanks

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Thu Aug 06, 2009 5:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Ok, you are using the 'old' validator. In this case you are bit by the problems described here: viewtopic.php?f=9&t=989793

There are some recommended workarounds on the thread. Otherwise I might be an option to upgrade to Hibernate 4.x based on Bean Validation.

--Hardy


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Thu Aug 06, 2009 6:40 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
I'm afraid it is not easy to solve in my case.

I have read the thread [url]viewtopic.php?f=9&t=989793[/url], but I need to export the constraints to DB, so I can't disable this feature. Although I have tried it and it works fine...

I would like to upgrade the validator used. You suggest one based on Hibernate 4.x, but the latest (stable) release is 3.1.0 GA, based on core 3.3.x, instead of 3.2.x (3.0.0 GA).

But, even upgraded to 3.1.0 GA, I have compatibility problems with other dependencies of my project, and this error is raised: java.lang.NoSuchMethodError : org.hibernate.event.PreInsertEvent.getSource()Lorg/hibernate/event/EventSource

Before re-structuring my POM files, I would like to be sure about the (original) problem solution.

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Thu Aug 06, 2009 8:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

have you tried the '@EntityListeners( JPAValidateListener.class )' approach?


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Thu Aug 06, 2009 8:54 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Hi again.

I have annotated my POJO with @EntityListeners(JPAValidateListener.class), and I have set hibernate.validator.autoregister_listeners property to false. But I don't have set hibernate.validator.apply_to_ddl property to false, but default.

With this configuration, the problem persists: null value is not detected by listener so UndeclaredThrowableException is still been raising.

What am I doing wrong?

Regards.

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Thu Aug 06, 2009 9:24 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Oops, I have been testing my validator. Now, with hibernate.validator.autoregister_listeners=false and @EntityListeners(JPAValidateListener.class), my validator does not validate!!!

Definitely I am doing somethig wrong. How can I use @EntityListeners(JPAValidateListener.class) correctly?

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Mon Mar 15, 2010 12:33 pm 
Newbie

Joined: Mon Mar 15, 2010 12:16 pm
Posts: 1
Hi all.

I've recently found this error in my compilations.
I'm dealing with Spring 3.0.1, Hibernate 3.2.7.ga (HBase) and Hibernate Core 3.3.2.GA (HCore).

I've dive into the sources for these two Hibernate packages and I've found something that I don't knonw whether can help or not to solve this issue. The: org.hibernate.event.PreInsertEvent.getSource() method, in Hibernate module, returns an instance of: org.hibernate.engine.SessionImplementor, while the: org.hibernate.event.PreInsertEvent.getSource() method, in Hibernate Core module, returns an instance of: org.hibernate.event.EventSource from the inherited class: AbstractPreDatabaseOperationEvent.

Apparently, both modules, Hibernate and Hibernate Core, has defined inside different objects with the same notation: org.hibernate.event.PreInsertEvent.
And, this is not only a notational problem, but also both of them has the same method: "getSource()" that returns different objects.

The problem is that, depending on the classpath location order for these files, the compilation may or not work properly.

I guess this problem can be solved simply just by renaming one of these methods.

Hope this can help.

PIPOHB


Top
 Profile  
 
 Post subject: Re: @NotEmpty does not work for null values
PostPosted: Tue Mar 16, 2010 4:48 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Hibernate 3.2.7.ga (HBase) and Hibernate Core 3.3.2.GA (HCore)

You really shouldn't have both on classpath.

BTW there's no need to solve the problem above as it applies to an old version only.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.