State Of Object During Hibernate

Posted By : Kuldeep Kumar | 19-Dec-2017

Hey Friends, Am Writing this blog to explain what are the different states of persistence class object during storage in the database.
In the Hibernate client module, the persistence Class Object can exist in one of the three states.


(1) Transient state
(2) Persistent state
(3) Detached state


1) Transient state:- As the persistence class Object is created, first of all, it moved in this state.in this state the persistence class Object never have the identity value.that means Object does not represent any database table record.


2) Persistent state:- In this state the hibernate POJO class Object contains the identity value represents the database table record having the synchronization.
In the Hibernate programming, the total ORM(Object Relation Mapping) persistence logic will be written based on this state Object.
The Objects in this state resides inside the persistence context.


The logical memory is in the client application where all the persistent state Object are there is called "Persistence context"


3) Detached state: -When the persistence context is closed as the result of the session.close() method or record is deleted from the database table or database or DBMS is shut down then he persistent state Object becomes the detached state Object. In this state, Object contains the identity value but does not represent the database table record.
 

Any transient state Object can be moved in the persistent state when the following methods of Session are Invoked they are:

 

1) session.save()

this method is used to insert the entity into the database.and return an integer value

 

2) session.persist()

this method is also used to insert the record in the database but the difference is that save() returns the generated id value and persist() does not return the generated id value.

 

3) session.merge()

merge() will insert the new record if existing record is not found for the updation.The merge method also returns the refId of the persistent state object represent the inserted or updated record.

 

4) session.update()

update() method will first look for the existing record if found then update the record value and if the record does not foud then it will generate the HibernateException


5) session.saveOrUpdate()

It works same as merge() method but not return the persistent state object representing the inserted or updated record in the database.


Any persistent state object can be sent to detached state by invoking following methods of session

1) session.delete()
2) session.evict()
3) session.close()

 

this method closes the connection between java application and database software and close the persistence context and makes all the persistent state objects as the detached state object.

 

About Author

Author Image
Kuldeep Kumar

Kuldeep is interested in developing web applications and he is currently working on Groovy on Grails. He likes to learn new technologies and love playing chess.

Request for Proposal

Name is required

Comment is required

Sending message..