Quick Contact

    Hibernate

    Hibernate Object States – Transient,Persistent and Detached

    Here we will discuss what is Hibernate object states with examples. Hibernate defines and supports the following object states:

    • Transient
    • Persistent
    • Detached

    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 =newStudent("Rajan", "Sharma", "rajansharma@ducatindia.com");
      Student s2 =newStudent("Priya", "Madan", "priyamadan@ducatindia.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:

    • Studentstudent=newStudent("Rajan", "Sharma", "rajansharma@ducatindia.com");
      Student student1 =newStudent("Priya", "Madan", "priya@ducatindia.com");
      Transactiontransaction=null;
      try (Sessionsession=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();
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.