09 - Working with Objects in Hibernate: Page 3 of 4

9.3.3 Modify an Object in database.

Most of the developers assumes that “ update() “ method of session object is used to update the state of an on object but this is wrong. update method is designed for something else ( we will cover this later in this chapter). It is important to understand and know that states of an object automatically gets updated in database when

  1. On  calling session.flush()
  2.  On calling transaction.commit()

We need  not to call any method explicitly to update the state.

Lets write UpdateBook.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tutorial.hibernate.Book;

public class UpdateBook {

       public static void main(String args[])
       {
         Configuration cfg = new Configuration().configure();        
         SessionFactory factory = cfg.buildSessionFactory();
         Session session = factory.openSession();
    
         Transaction tx = session.beginTransaction();
         Book book = (Book)session.get(Book.class, 3);

         book.setAuthor("Author 2");

         tx.commit();
        factory.close();
    }
}

Run UpdateBook.java and we can see that update statement is fired even though we did not call any update method.

 

9.3.4 Delete Persistent Object from database.

To delete any persistent object, we can call delete() method passing the object to be deleted.

Delete() method  also changes the state of a persistent object to transient state.

Lets write DeleteBook.java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tutorial.hibernate.Book;

public class DeleteBook {

      public static void main(String args[])
      {

        Configuration cfg = new Configuration().configure();        
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Book book = (Book)session.get(Book.class, 3);

        session.delete(book);

        
        tx.commit();
        factory.close();
    }
}

Run DeleteBook.java

 

9.4 Reattach a Detached Object OR modifying detached Object.

In any of web application we may need to reattach the detached object. Think of the situation when back end system loads the object from database and closes the session. The object gets modified by another layer and requires update in the database. To reattach the detached object there are two options –

  1. Update() – as mentioned earlier, this method is not designed to update the state of an object instead it is designed to reattach the detached objects.

       If session already contains a persistent object with same identifier, it will throw an exception.

       Update () returns void

  1. Merge() – merges the modifications with the persistent object with same identifier and returns the reference of new object. We should use merge if we want to update the changes without considering the state of a session.   The returned object is part of persistent context and tracked for any changes where as the  passed object will not be associated with the session.

      Note: closing a session() will make all the object that were associated with that session to detached state.

Let's take an example.

Case 1 - Updatebook.java – Update / Reattach the detached object.

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tutorial.hibernate.Book;

public class UpdateBook {

     public static void main(String args[])
     {

        Configuration cfg = new Configuration().configure();        
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        Book book = (Book)session.get(Book.class, 4);

        session.close();

        book.setName("Hibernate  Tutorial Updated !!!");

        Session session2 = factory.openSession();

        Transaction tx = session2.beginTransaction();

        session2.update(book);

        tx.commit();
        session2.close();
        factory.close();
    }
}

Run UpdateBook.java

 

Case 2 - Updatebook.java – Update Object in same session using update

Though this will update the data but this is wrong approach and this is what we discussed earlier that update() method should not be used for such purpose as hibernate will automatically fire update statement.

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.tutorial.hibernate.Book;

public class UpdateBook {

     public static void main(String args[])
     {
        Configuration cfg = new Configuration().configure();        
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();        
        Book book = (Book)session.get(Book.class, 4);        
        book.setName("Hibernate  Tutorial Updated Again !!!");

        session.update(book);

        tx.commit();
        session.close();

        factory.close();
    }
}

 

Case 3 - Updatebook.java – Update Object with same identifier which is already in session

As I pointed out in description of update() method that if object with same identifier with the object we are updating is available in session, it will throw an exception.

 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tutorial.hibernate.Book;

public class UpdateBook {

      public static void main(String args[])
      {
        Configuration cfg = new Configuration().configure();        
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();
        Book book = (Book)session.get(Book.class, 4);
  
        session.close();

        Session session2=  factory.openSession();        
        Transaction tx = session2.beginTransaction();        
        Book book1 = (Book)session2.get(Book.class, 4);            
        book.setName("Hibernate  Tutorial Updated Again !!!");

        session2.update(book);
   
        tx.commit();
        session2.close();

        factory.close();
    }
}

Case 4 - UpdateBook.java – Loading object twice and update one of them

Below code will not throw error because book1 and book2 will refer to same object and it is like updating the same object so though it looks like we are updating an object with same identifier.

Session session2=  factory.openSession();        
Transaction tx = session2.beginTransaction();
Book book1 = (Book)session2.get(Book.class, 4);    
Book book2 = (Book)session2.get(Book.class, 4);        
book2.setName("Hibernate  Tutorial Updated Again !!!");                
session2.update(book1);
}

Case 5 - MergeBook.java 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.tutorial.hibernate.Book;

public class MergeBook {

       public static void main(String args[])
       {
         Configuration cfg = new Configuration().configure();        
         SessionFactory factory = cfg.buildSessionFactory();
        
         Book book = (Book)session.get(Book.class, 4);

         session.close();

         Session session2=  factory.openSession();        
         Transaction tx = session2.beginTransaction();                
         book.setName("Hibernate  Tutorial !!!");
   
         session2.merge(book);
         tx.commit();
         session2.close();

         factory.close();
    }
}

Run MergeBook.java 

 

Like us on Facebook