-->
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.  [ 6 posts ] 
Author Message
 Post subject: Weird Validator/BeanValidatorEventListener problem
PostPosted: Mon Oct 19, 2009 5:14 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
Hi again!

I have come across another weird problem, which may or may not be a bug. It occurs on Collections where I am trying to validate the size.

Here is the constraint:
Code:
   @Size( min = 1, message = "{job.categories.size}" )
   @NotNull( message = "{job.categories.size}" )
   private List<Category> categories = new ArrayList<Category>();


This validates 100% fine when not connected to Hibernate. However, if this is processed by Hibernate, we get a weird problem going on.

Here is the mapping in Hibernate for this collection:
Code:
        <bag name="categories" table="job_to_category" lazy="false">
            <key column="job_id"/>
            <many-to-many class="myproject.domain.admin.Category" column="category_id"/>
        </bag>


Now, when I submit a form backing object to a controller that contains this Job object that is 100% valid, I get this exception once Hibernate tries to commit/flush the session:

Code:
ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
   at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
   at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:767)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:736)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:394)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy26.editJob(Unknown Source)
   at myproject.web.controllers.employer.JobsController$2.onSubmit(JobsController.java:150)
   at myproject.web.helper.ProcessForm.submit(ProcessForm.java:44)


Frankly, I'm at a loss as this exception does not happen if I turn off the @Size validation. I have read that this means an object was deleted out of the session before Hibernate gets to it... but how could that happen? There are no objects being deleted here, and there is no cascading on "categories" property :(

Please help.


Top
 Profile  
 
 Post subject: Re: Weird Validator/BeanValidatorEventListener problem
PostPosted: Tue Oct 20, 2009 2:39 am 
Hibernate Team
Hibernate Team

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

I could image that the problem is
Code:
private List<Category> categories = new ArrayList<Category>();


But it depends on the rest of the class and how your code between opening and closing the session looks like. It could be that Hibernate treats the creation of the categories list as a 'replacement' for an existing persisted list. This could happen if modifications happens in a detached state.

It would help to see the full mapping and the actual session code. I also recommend to turn on SQL debug trace to be able to follow what SQL gets executed. Also turn of batch updates. This gives often a better SQL error message.

--Hardy


Top
 Profile  
 
 Post subject: Re: Weird Validator/BeanValidatorEventListener problem
PostPosted: Tue Oct 20, 2009 3:50 am 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
How does one initialize the collection then if not there? The get() property? There are several addXyz() methods that assume the collection is instantiated in my project. They manage collection/relationship management. I don't know how to not instantiate the empty list :(


Top
 Profile  
 
 Post subject: Re: Weird Validator/BeanValidatorEventListener problem
PostPosted: Tue Oct 20, 2009 5:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Well, you could add some method which checks whether the collection is null before you add something to it. That said, it might not be the cause of the problem. I think the first step would be to look at the debug trace.
Are you using the Open session in view pattern in your application?

--Hardy


Top
 Profile  
 
 Post subject: Re: Weird Validator/BeanValidatorEventListener problem
PostPosted: Tue Oct 20, 2009 6:24 pm 
Regular
Regular

Joined: Tue May 12, 2009 6:08 am
Posts: 92
The problem went away. I don't think I know what happened... but I suddenly don't have the problem anymore. LOL.

I think I changed the test data. There may have been a record in my test database that did not actually fulfill the validation constraint... so when loading it on the form and then saving it with the proper results... maybe it was having trouble?

That's the only thing I can think of as the error just disappeared. Works great now. Perfectly. LOL ;)


Top
 Profile  
 
 Post subject: Re: Weird Validator/BeanValidatorEventListener problem
PostPosted: Wed Oct 21, 2009 2:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Fair enough. I could imagine inconsistent data causing this problem as well. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.