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.setEname("sandeep");
instance.setJob("SE");
instance.setMgr(new Integer(6));
instance.setSal(546545646.89898);
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*/
instance.setDept(dept2);

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 */

emp.setEname("hariesh");
empHome.attachsaveorupdate(emp);

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.