Hello friends, I am again in your service with this important blog. In this blog I will explain the various state of hibernate persisting object. In provided image you will see all the states in GUI format which gives you a rough idea.

You see there are three state of persisting object in hibernate. Lets introduce you with these states.

  • Transient State : – An object is in transient state when we create object with new operator.e.g:-
    Transient transientObject = new Transient()

    When we call any of the method(remove(), delete()) on persistent object then it also moves to transient state.e.g:-

    session.delete() or session.remove()

    In this state hibernate is not aware of this object. This object not represent any database row. This object is available for garbage thread if no reference found.

  • Persistent State : – When we call any of the method(save(), saveOrUpdate(), persist(), merge()) on transient object then it will moved to persistent state.e.g:-
    session.save() or session.saveOrUpdate() or session.persist() or session.merge()

    If we call (get(), load(), find()) method on hibernate session then already saved database object will moved to persistent state.e.g:-

    session.get() or session.load() or session.find()

    If we call(update, saveOrUpdate(), merge()) method on detached object then the object move to the persistent state.e.g:-

    session.update() or session.saveOrUpdate() or session.merge()

    In this state object known to the hibernate and represent a row in the database. If we will make change in object property it will also reflect to the database without invoking session persistent method. Only this is the state in which object is saved to database.
    Note :- In case of ‘load()’ method we have to call session persistent method explicitly.

  • Detached State : – When we call any of the method(evict(), clear(), close()) on persistent object then it moves to detached state.e.g:-
    session.evict() or session.clear() or session.close()

    The persistence object that can be referenced to database after closing the hibernate active session. Changes made in this object doesn’t reflect to the database. But we can change the state to persistent by calling (update(), saveOrUpdate(), merge()) method on detached object. e.g:-

    session.save() or session.saveOrUpdate() or or session.merge()

    If this detached object is not having any reference then it will be available to garbage thread.

Hope it helps.