Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes StaleStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "StaleStateException" Watch "StaleStateException" New topic
Author

StaleStateException

subhashchandra medhiassam
Ranch Hand

Joined: Sep 10, 2008
Posts: 72
hi all,
There are three classes in my application. They are:

Author.java:


Book.java:



and InsertRecord.java:



The configuration files are:
author.hbm.xml:



and hibernate.cfg.xml:



When i run InsertRecord.java, two tables, author and book are getting created in the MYSQL database but they are not getting populated with data and the following message is being generated:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into Author (name, id) values (?, ?)
Hibernate: update Book set name=? where id=?
Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
Hibernate: update Book set id=? where id=?
Hibernate: update Book set id=? where id=?
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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:669)
at roseindia.tutorial.hibernatecollections.InsertRecord.main(InsertRecord.java:43)

I would like to know why the tables are not getting populated. Kindly let me know where i went wrong.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well you are not supposed to set the ids. You chose generator type="increment" which says to me that the database has an auto-increment field for that id. And also that Hibernate will only do an insert if the id in your object is not set. If there is an id, it will do an update instead. So for your Books, you set the id, so Hibernate tries an update statement, but there seems to be no record in the Book table with that id, so the update statement fails to update anything, the update statement returns 0 for number of rows affected, and that makes Hibernate throw an Exception.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Please don't duplicate post, I deleted your duplicate.

Mark
subhashchandra medhiassam
Ranch Hand

Joined: Sep 10, 2008
Posts: 72
You were correct. When i removed the statement "book1.setId(1);", the program worked fine.

But now, when i am trying to add details for another book enity, i am again getting the same StaleStateException.

The new code looks like this:




The output message is:


Please help me out.
subhashchandra medhiassam
Ranch Hand

Joined: Sep 10, 2008
Posts: 72
Hi all,
Kindly help me.

Thanks in advance,
Subhash
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Can you change

<bag name="books" cascade="all" >
<key column="id"/>
<one-to-many class="roseindia.tutorial.hibernatecollections.Book"/>
</bag>

To use <list> instead of <bag>?

Here is the Hibernate docs too.

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-unidirectional-12m

Mark
subhashchandra medhiassam
Ranch Hand

Joined: Sep 10, 2008
Posts: 72
Hi,
I changed the file author.hbm.xml. I am now using "list" instead of "bag". The file now looks like the following:



But when i run the program, i am still getting the StaleStateException. The message looks like this:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into Author (name, id) values (?, ?)
Hibernate: update Book set name=? where id=?
Hibernate: update Book set name=? where id=?
Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
Hibernate: update Book set id=?, idx=? where id=?
Hibernate: update Book set id=?, idx=? where id=?
Hibernate: update Book set id=?, idx=? where id=?
Hibernate: update Book set id=?, idx=? where id=?
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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:669)
at roseindia.tutorial.hibernatecollections.InsertRecord.main(InsertRecord.java:43)


Kindly tell me what to do now. Please help me as i am out of my wits.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

For some reason, you still have ids being set in the Book objects. If the id is null then Hibernate will do inserts, if it is not null it will do updates. and Hibernate is doing updates

If you have
Update blah where id = 1 and there is no record in the database already with that id then it will fail.

Did you recompile and not that it is still using an old version of your code?

Mark
subhashchandra medhiassam
Ranch Hand

Joined: Sep 10, 2008
Posts: 72
This is the code:



when i execute it, i still get the following message:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into Author (name, id) values (?, ?)
Hibernate: insert into Book (name, id) values (?, ?)
Hibernate: insert into Book (name, id) values (?, ?)
Could not execute JDBC batch update
Hibernate: update Book set id=?, idx=? where id=?
Hibernate: update Book set id=?, idx=? where id=?
Hibernate: update Book set id=?, idx=? where id=?
Hibernate: update Book set id=?, idx=? where id=?
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 1 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:88)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:74)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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:669)
at roseindia.tutorial.hibernatecollections.InsertRecord.main(InsertRecord.java:43)


What could be the reason? Kindly help me.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Let's try this. Remove " unsaved-value="0" " from your id tags. No reason to have this at all.

Mark
subhashchandra medhiassam
Ranch Hand

Joined: Sep 10, 2008
Posts: 72
I tried. but still it did not work.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

I am sorry but I don't see anything specific in you code you posted. But the only way Hibernate ever tries to do an update is if the id field of the Java object is not null. So somehow, maybe in that class you are setting the id field in that code. But if it is null Hibernate will do an insert, if it is not null it will do an update. And since it is doing an update, it is being set somehow.

I am Sorry I wasn't of anymore help for you.

Good Luck

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StaleStateException
 
Similar Threads
How to insert using hibernate?
data insert error
session.flush(); gives null
Is my approach right while inserting a foreign key?
MappingNotFoundException