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);

1 comment: