Bech on Enterprise Java
Friday, June 23, 2006
  Hibernate: Could not synchronize database state with session, Unexpected row count: 0 expected: 1
If you get this error message during a session.flush();

Could not synchronize database state with session
org.hibernate.HibernateException:
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:32)

And let's assume you're working with a Persistent object with a generated primitive key, and an "unsaved value" strategy. Your problem is probably that you forgot to initialize the private field used as an id to the "unused value". This means that you should never never never write a persistent class like this ;

public class MyClass () {
private int id ;
}

But rather do this ;

public class MyClass () {
private int id = -1 ;
}

One of those stupid mistakes that can snaeak in and cause some minutes of headache. Did you get here by searching for the exception, and this solved your problem, please leave a comment! :-)
 
Comments:
Yep, we ran into this problem a number of times recently. It is very important to understand the logic hibernate uses to persist objects. In our case, we would attempt to add a child object while the parent object that it referenced did not have a persistable id. It is tougher to debug when it is happening on a different object than the one you are trying to save.
 
Yes, yes , yes... this article helped me to find a gooooood bug
 
Oh my god... THANKS!!!
 
Oh my god... THANKS!!!
 
有可能是数据库里有两条ID相同的记录
 
I was using spring to copy properties from a staging object (previously inserted) to 2 new objects to be inserted... of course, BenUtils.copy also included the id :) and of course hibernate was having some problems with that
 
Hi, I have the problem AbstractFlushingEventListener:300 - Could not synchronize database state with session when I'm using a Map but child table haven't primId. How I can fix it? Can you help me ? Thank you.
 
It helped me to realize, that session.saveOrUpdate(object) requires the id to be 0. In my case, I used the id for internal identifying and had assigned negative values (-1, -2, ..., -n) to the objects. Therefore, Hibernate wasn't able to save the object; the id needed to be 0 for new (transient) objects.
And yes, I googled for the errormessage and found this blog helpful! ;-)

regards, Björn
 
Yes, fixed my problem. I had all my ids in POJOs declared like this.
 
Yes, fixed my problem too: I assumed hibernate would commit any uncommitted referenced objects automatically.

I had to commit all referenced objects first, then commit the referencing object.
 
thanks for the tip. Really appreiated!
 
If u r using generator class='increment' in ur .hmb file then also u will see this problem, as increment option is not cluster. thread safe. I used sequence and now everything is working fine for me.
 
If u r using generator class='increment' in ur .hmb file then also u will see this problem, as increment option is not cluster. thread safe. I used sequence and now everything is working fine for me.
 
thanks
 
thank you, it solved the problem.
 
Absolutely the same, thanks a lot!
 
I have below function:

public void saveObject(final Object o) {
ht.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO );
ht.saveOrUpdate(o);
ht.flush();
}
It's successful if updating
In case of adding new, i get error:
2007-05-30 17:11:56,433 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
org.hibernate.HibernateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:32)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1982)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.springframework.orm.hibernate3.HibernateTemplate$27.doInHibernate(HibernateTemplate.java:713)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:711)
at jp.go.gwe.base.BaseDAOImpl.saveObject(BaseDAOImpl.java:129)
at jp.go.gwe.sd.service.impl.AreaServiceImpl.txSaveArea(AreaServiceImpl.java:60)
at jrockit.reflect.VirtualNativeMethodInvoker.invoke(Ljava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source)
at java.lang.reflect.Method.invoke(Ljava.lang.Object;[Ljava.lang.Object;I)Ljava.lang.Object;(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:288)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy36.txSaveArea(Ljp.go.gwe.sd.model.MArea;)V(Unknown Source)
 
Yes, it helped. Stupid me added a @GeneratedValue to an id i wanted to set myself -> same Exception.
 
You will enjoy knowing that 15 months ater your original post, it is the #1 google hit for the exception text ("Could not synchronize database state with session").

And, the answer is still true.

Props to Google, Blogger and Bech.

Sept 19 2007
 
It Didn't work for me :(. I have composite primary key not any DB generated auto primary key. And all the combination will come from Screen i.e. user entered.
 
Post a Comment



<< Home
I hereby promise to blog my thoguhts and views on Enterprise java, design patterns, frameworks and all other things that make life as a software developer interesting.

Name:
Location: Oslo, Oslo, Norway

www.glennbech.com

ARCHIVES
May 2005 / June 2005 / July 2005 / January 2006 / February 2006 / June 2006 / July 2006 / August 2006 / October 2006 / November 2006 / March 2007 /


Powered by Blogger