objSession.beginTransaction(); Users objUser = new Users(); objUser.setUserName("Max"); . . . ----Transient State---- . . objSession.save(objUser); . . . ----Persistent State---- . . objUser.setUserName("Max Muller"); . . . ----Persistent State---- . . objSession.getTransaction().commit(); objSession.close(); . . . ----Detached State---- . . //Doesnot get reflected objUser.setUserName("Max Muller"); . .
- The object will be in Transient State until it is saved.An object is in transient state if it just has been instantiated using the new operator and there is no reference of it in the database i.e it does not represent any row in the database.
- The object will be in Persistent State until the session is closed.An object is in the persistent state if it has some reference in the database i.e it represent some row in the database and identifier value is assigned to it. If any changes are made to the object then hibernate will detect those changes and effects will be there in the database that is why the name Persistent. These changes are made when session is closed. A persistent object is in the session scope.
- The object will be in Detached State once the session is closed.An object that has been persistent and is no longer in the session scope. The hibernate will not detect any changes made to this object. It can be connected to the session again to make it persistent again.
The changes done after the session close will not get reflected