Saturday, 15 February 2014

Transient, Persistence and Detached Objects in Hibernate

While an entity class is created to create a database table or an entity, it is done by making an instance of the entity class and by assigning this instance to the session instance, database table schema is added or updated into the database and a table entry is made. Once the session transaction is saved, it is required to release the entity class instance. These three states of object are classified into their specific categories as Transient, Persistence and Detached object. This article will dictate these all three states of an entity class and will go with an example on this.

Transient, Persistence and Detached Objects in Detail:
Once an entity class has been created to create a database entity or table, it is required to connection with the hibernate and database using SessionFactory and its set of classes. Once the connection configuration is done, we are required to create an instance of the entity class. This state of instance is known as Transient Object.To save the entity class instance into the database, we are required to create a Session class instance that will use the method save() to save the entity class instance into the database. This configuration of the entity class instance is known as Persistence Object.It is necessary to close the session instance after performing the database task. Once a session instance is closed, the state of entity class instance is set to Detached Object.

Let’s go with an example that will dictate the use of transient, persistence and detached object as started with Listing 1:

Listing 1:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
public class UserDetails {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private intuserId;
    private String userName;
    public intgetUserId() {
        return userId;
    public void setUserId(intuserId) {
        this.userId = userId;
    public String getUserName() {
        return userName;
    public void setUserName(String userName) {
        this.userName = userName;
Listing 2:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class AddRecords {
    public static void main(String[]args){
        SessionFactorysessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        //To create data
        for(int i = 0; i < 10; i++){
            UserDetails user = new UserDetails();//Creating Transient Object
            user.setUserName("user" + i); //Setting up values to the transient object
  ;//Updating the transient object to persistnence object
        session.close(); //Session is closed
A new instance of a a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transient by Hibernate:

Person person = new Person();

// person is in a transient state
A persistent instance has a representation in the database, an identifier value and is associated with a Session. You can make a transient instance persistent by associating it with a Session:

Long id = (Long);
// person is now in a persistent state
Now, if we close the Hibernate Session, the persistent instance will become a detached instance: it isn't attached to a Session anymore (but can still be modified and reattached to a new Session later though).


Post a Comment

Related Posts Plugin for WordPress, Blogger...