Monday, September 19, 2011

Identifying the state of hibernate objects practically

Emp instance=new Emp(); /* creating the transient object using the new operator which is not attached to database , which has to be saved in db usinh save or saveorupdate method of session object from sessionfactory */
instance.setEmpno(new Integer("111111"));
instance.setMgr(new Integer(6));
instance.setHiredate(new Date());
com.DeptHome deptHome=new com.DeptHome();
Dept dept2=deptHome.findById(40);/* userdefined method findById persist object which is attached with session need to update not to save*/

empHome.attachDirty(instance);/*user defined method conatins save method of session object to save instance to db*/

Emp emp=empHome.findById(7942);/* findById is user defined method to retrive the emp object using the emp id hence emp object is persist object attached to session which can be modified and save into database using the saveorupdate method of session object */


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(
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(
at org.hibernate.jdbc.AbstractBatcher.executeBatch(
at org.hibernate.engine.ActionQueue.executeActions(
at org.hibernate.engine.ActionQueue.executeActions(
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(
at org.hibernate.impl.SessionImpl.flush(
at org.hibernate.impl.SessionImpl.managedFlush(
at org.hibernate.transaction.JDBCTransaction.commit(
at com.EmpHome.attachsaveorupdate(
at com.EmpHome.main(

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
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.
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.
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.