Lesson - 20 : Hibernate - Object states in Hibernate

preview_player
Показать описание
In hibernate application, a pojo class object can be in any one of the following three states.
1. Transient state
2. Persistent state
3. Detached state

Transient state:
1. When a pojo class object is newly created or a null value is assigned for the object then the object will be in a transient state.
2. For ex: Product p = new Product(); //p is in transient state.
Product p= null; //p is in transient state
3. A transient state object is not associated with a session object of hibernate so it is not associated with a database also.
4. If we make any changes on a transient state object then the changes are not reflected on under laying database.
5. If a pojo class object is new then it can be converted from transient state to persist state by doing save operation on the object.
6. To do save operation , the methods are
save()
persist()
saveOrUpdate()
7. If a pojo class object is assigned with null then it can be converted to persistent state by doing load operation.
8. To do load operation , the methods are
load()
get()

Persistent state
1. When a pojo class object is added to the session cache then the object will be in a state called persistent state.
2. If you make any changes on persistent state object then the changes are effected on (or) applied on databases also.
3. A persistent object can be moved to either detached state or transient state.
4. To move a persistent state objectto detachedstate then we needto call one of the following methods
evict()
clear()
close()
5. To move an object from persistent state back to transient state then we need to call delete() method.

Detached state
1. When an object comes out of an session cache then it will be entered into detached state.
2. If any changes are made on detached state object then the changes are not effected on database.
3. A detached state object can be moved to persistent state by calling one of the below methods
update()
merge()
saveOrUpdate()
4. If a detached object is not converted to persistent state then finally it goes to garbage

Рекомендации по теме