Engineering Full Stack Apps with Java and JavaScript
A java object within a hibernate application can be in one of the three states:
New/transient
Attached/persistent
Detached
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();
u.setId(1);
u.setName("Heartin");
Later we save it within a session as:
session.beginTransaction();
session.save(u);
session.getTransaction().commit();
Modify the object after session.save as:
session.beginTransaction();
session.save(u);
u.setName("Heartin.Jacob");
session.getTransaction().commit();
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.