Hibernate Object States – Transient,Persistent and Detached

In this article, we will discuss what are Hibernate object states with examples. Hibernate defines and supports the following object states:
  • Transient
  • Persistent
  • Detached
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial

Transient

  • An object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. For example, here we are creating a Student object using new operator and note that we are not saving student object to a database using Hibernate Session.
Student s1 = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
Student s2 = new Student("John", "Cena", "johncena@javaguides.com");
  • It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore.
  • Newly instantiated instances of a persistent class are considered transient by Hibernate. We can make a transient instance persistent by associating it with a session:
        Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
        Student student1 = new Student("John", "Cena", "john@javaguides.com");
        Transaction transaction = null;
        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            // start a transaction
            transaction = session.beginTransaction();
            // save the student objects
            session.save(student);
            session.save(student1);
            // commit transaction
            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) {
                transaction.rollback();
            }
            e.printStackTrace();
        }

Persistent

  • A persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session.
  • Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements or DELETE statements when an object should be made transient.
  • Newly instantiated instances of a persistent class are considered transient by Hibernate. We can make a transient instance persistent by associating it with a session. Below diagram shows the snippet of saving an entity in a database:
If we want to save an object into a database then we need to call any one of the following 3 methods:
  • save()
  • persist()
  • saveOrUpdate()

Detached

A detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state.
A detached instance can be reattached to a new Session at a later point in time, making it (The update() and merge() methods are used to reattach the detached objects to a session) persistent again.
We can detach an object from the session by using the three methods.
session.clear();
session.evict(student);
session.close();
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial

Comments