There was an error in this gadget

Monday, September 19, 2011

org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

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:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.EmpHome.attachsaveorupdate(EmpHome.java:137)
at com.EmpHome.main(EmpHome.java:76)


Solution
This Exception will arise in different scenario of save and saveOrupdate method of hibernate.
if Object is transient , need to save may occur this exception
Conditions.
1. Flushing the data before committing the object may lead to clear all object pending for persist.
2. If object has primary key which is auto generated and you are forcing has assigned key may cause the exception.
3. if you are cleaning the object before committing the object to database may lead this exception.
4. Zero or Incorrect ID:
Hibernate excepts an primary or id of null (not initialize while saving) has per point 2 to mean the object was not saved. If you set the ID to zero or something else, Hibernate will try to update instead of insert, or it lead may to throw this exception.
5.
Object Doesn’t Exist: This is the most easy to determine : has the object get deleted somehow? If so, trying to delete once again it will throw this exception.
6.
Object is Stale: Hibernate caches objects from the session. If the object was modified, and Hibernate doesn’t know about it, it will throw this exception — note the StaleStateException part of the exception.

so after committing the object to database need to flush or clear or clean it ,not before.

11 comments:

  1. This problem mainly occurs when we are trying to save or update the object which are already fetched into memory by a running session. For e.g. if i am trying to fetch an object into memory and setting the members of that object into another object then this prob will occur.

    for e.g. class person's object is a member of class men then if i fetch the object of men and set it to another object and trying to save the new object will raise this exception.

    ReplyDelete
    Replies
    1. So what is the solution for this? As you said I'm doing the same thing, I have all ready fetched an object into memory and copying the members of that object into another.

      Delete
  2. what's the solution

    ReplyDelete
  3. So what do you mean by "committing the object" What is the syntax of the statement to do that? Are you talking about "session.Commit" or something else?

    ReplyDelete
    Replies


    1. session.flush();
      session.gettransaction.commit();

      Delete
  4. Stale state means
    Hibernate caches objects from the session. If the object was modified, and Hibernate doesn’t know about it then this exception is thrown. This will mainly occurs when 2 or 3 user try to acquire the object and modify but others are not reflected with that state.


    1. This can be resolved by updating the object from database like refresh method in hibernate and then commit will persist the object in db.

    2. You can use merge method and commit, But it is risky since it will override with current data.

    3. Using lock can also be resolved.

    ReplyDelete
  5. Welcome Post very usefull informatation.and

    iam expecting more posts like this please keep updating us........

    ReplyDelete