Object States in Hibernate 4.3

 A java object within a hibernate application can be in one of the three states:

  1. New/transient

  2. Attached/persistent

  3. Detached


Important points about object states in Hibernate

  • When an object is created, it will be in the new/transient state.

    • Hibernate will not save any updates to the transient objects.

  • When you do a get, save, update or delete from within a session transaction, the object moves to the persistent state.

    • Now the object will be attached to the session and any changes will be tracked by hibernate.

  • Finally when you do a session.commit(), object moves to the detached state, doing the following:

    • Hibernate will save the latest version of the object.

    • The object is detached from the session.

  • Hibernate will not save any updates to the detached objects, until they are attached again to a session.



Make below modifications over the lab excercise done @ http://javajee.com/lab-your-first-hibernate-43-program

Make below modifications in the test class alone:

Initially we created an object and gave values as:

User u = new User();



Later we save it within a session as:





Modify the object after session.save as:





If you execute the same program now, you can see that, the value of the name field is updated to Heartin.Jacob, even though there is no explicit save command.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!