Hibernate Object Life - TopicsExpress



          

Hibernate Object Life Cycle: ======================================================== In hibernate object life cycle, mainly consists four states. They are 1. Transient, 2. Persistent, 3. Detached and 4. Removed states. Before going to discuss about Hibernate Object Life Cycle, i am taking small example i.e Relationship between me and my girl friend. Initially there is no relationship between me and my girl friend. I am thinking to love my girl friend. ( here my love is started i.e, new state). After some days, i proposed to my girl friend. She is accepted so now relationship is happened both sides.( here there is relationship b/w me and my gf). After some days due to some reasons my love is breakup ed. ( here now there is no relationship b/w me and my gf but previously there is a relationship). i have some hope she will meet once again. i waited some days. If she is not came and meet. Now my relationship will be gone ( may be after some age i will die) I think you are understand the me and my gf relationship. here if there relationship then only we both will meet. Now, we can go our actual Hibernate Object Life Cycle. a. New or Transient State: --------------------------------------------------------------------------- When ever an object of a pojo class is Created(instantiated) using the new operator then it will be in the Transient state; this object is not associated with any Hibernate Session. For Example, Employee employee = new Employee(Ranga, 25, 30000); This object don’t have any association with any database table row. In other words any modification in data of transient state object doesnt have any impact on the database table. so their state is lost as soon as they’re no longer referenced by any other object. Transient objects exist in heap memory. Transient state will be happened two scenarios – first where the objects are created by application but not connected to a session, and second the objects are created by a closed session. Transient State to Persistence State ============================================= ○ By saving the that object save() persist() saveOrUpdate() ○ By loading that object from database load() get() etc.. b. Persistent or Managed State: --------------------------------------------------------------------------- When the object is in persistent state, then it represent one row of the database, and it is associated with the unique Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. We can create persistent objects via two ways: 1. Loading the object from the database using Session 2. Saving the object to the database using Session Ways to Save an Object ---------------------------------- Hibernate has a few different ways to save an object to the database. They are save() saveOrUpdate() persist(); Invoking either of these Hibernate methods will shift your transient object into the persistent state (so long as the save is successful). For Example, Employee employee = new Employee(Ranga, 27, 399999); // Transient State because employee object is not associated with session object. session.save(employee); // Persistent State because employee object is associated with session object. Ways to Load an Object ---------------------------------------- There are quite a few ways to load an object from a database. get() load() byId() byNaturalId() getUniqueResult() etc... For Example, Employee employee = session.get(Employee.class, 1); // here employee object is associated with session. So employee object state is Persistent. Persistent State to Detached State ============================== session.clear(); session.close(); session.evict(); c. Detached State: ------------------------------------------------------------------- if we want to move an object from persistent to detached state, we need to close the session or clear the cache of the session. Here the reference to the object is still valid and the detached instance can be modified in this state. session.clear(); session.close(); session.evict(); Employee employee = new Employee(Ranga, 27, 30998); // Transient session.save(employee); // persitent session.close(); // here employee state is detached because currently it is not associated with session. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modification) persistent again. Detached State to Persistent State: ============================== update() merge() saveOrUpdate() d. Removed State ------------------------------------------------ A persistent object is considered to be in the removed state when a delete() operation is called on it. Note that Once youve deleted an object and moved to the “removed” state, you should no longer use that particular object for any reason. For Example: session.delete(employee); Note: For your understand only i given me and my girl friend relationship. If you are not understand leave it that concept. Below diagram u can find all states of Hibernate.
Posted on: Fri, 26 Dec 2014 13:43:25 +0000

Recently Viewed Topics




© 2015