aspose file tools*
The moose likes Object Relational Mapping and the fly likes Exception while inserting a row using Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Exception while inserting a row using Hibernate" Watch "Exception while inserting a row using Hibernate" New topic
Author

Exception while inserting a row using Hibernate

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
Hi ranchers,
I am new to Hibernate. I just tried a simple one to many association using an item class and a bid class. I somehow inserted a row into the item table using hibernate and later saw that the bid table was not updated with the record. So i deleted the record in the Item table from MySQL command prompt and tried to once again insert into Items table but i am getting the following error.

ERROR AbstractBatcher:51 - Exception executing batch:
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:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:218)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2289)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2242)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2542)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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 com.local.hibernate.unidirection.AuctionClient.main(AuctionClient.java:40)
19:07:38,396 ERROR AbstractFlushingEventListener:301 - 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:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:218)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2289)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2242)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2542)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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 com.local.hibernate.unidirection.AuctionClient.main(AuctionClient.java:40)

ItemBid.xml



Item.java class


Bid.java



hibernate.cfg.xml



AuctionClient.java class


I am using MySQL 5.0. Kindly find the code and help me out.

Thanks in advance,
Pradeep



Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Javid Jamae
Author
Ranch Hand

Joined: May 14, 2008
Posts: 198
This is just a guess, but you don't seem to be closing your session. So, what may have happened was that Hibernate still had the Item associated with the session. Then when you deleted it directly from the DB, Hibernate got confused.

Also, I notice you don't have inverse="true" set for your bi-directional relationship. I'm guessing you got an error before this one when you tried to persist the Item? That error would have occurred because Hibernate would have tried to write the ITEM_ID foreign key twice, once for the many-to-one and once for the one-to-many.


Author: JBoss in Action, javidjamae.com, @javidjamae on Twitter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception while inserting a row using Hibernate