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

9.3.2 Retrieve a persistent Object

Quite often application would need to load the data or we can say the persistent objects from database. Hibernate does provide two ways to get the persistent object

  1. Load () – does not make a call to the database instead gives a proxy object with an identifier value populated only and other properties remain uninitialized. In case row corresponding to given identifier does not exists, ObjectNotFoundException will be thrown.
  2. get()- method calls the database to find the object associated with the given identifier. In case object does not found, returns null.

 

9.3.2.1 load vs. get

  • We should call load when we are sure that row corresponding to the given identifier exists.
  • Load returns proxy and calls database only when required so gives a better performance
  • Get can be used to validate if row corresponding to identifier exists.
  • Load always returns proxy even if the row does not exist.  It throws Object not found exception when we make a call to any of properties of proxy object and row does not exist.
  • The use of load is recommended in case of associations where we just need an object with an identifier (not complete object) for relationships among objects. For example, the library has a list of books and we need just book’s identifier for a relationship. In case we need a complete object of the book, we should use get()

9.3.2.3 Example

 Let’s use the same Book object that we have used in Section 9.2 . Below is the state of book table  (two records with ISBN 3 and 4 )

GetBook.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 GetBook {

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

        System.out.println("Fetching Book with isbn number 3 ");
        System.out.println();
        Book book = (Book)session.get(Book.class, 3);
        System.out.println();
        System.out.println(book);
        System.out.println("Book Name =" + book.getName() );
        System.out.println("Book Author =" + book.getAuthor() );

        System.out.println();
        System.out.println("Fetching Book with isbn number 10 ");
        System.out.println();
        book = (Book)session.get(Book.class, 10);
        System.out.println();
        System.out.println(book);

        factory.close();
    }
}

Run GetBook.java 

You can verify that select statement is fired in case of get immediately and we are getting null object for ISBN=10 as it does not exist.

   

LoadBook.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 LoadBook {

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

        book = (Book)session.get(Book.class, 10);
        System.out.println();
        System.out.println(book);
  
        System.out.println("Fetching Book with isbn number 3 ");
        System.out.println();
        Book book = (Book)session.load(Book.class, 3);
        System.out.println();
        
        System.out.println("Fetching Book with isbn number 10 ");
        System.out.println();
        book = (Book)session.load(Book.class, 10);

        factory.close();
    }
}

Run LoadBook.java

   In LoadBook.java we have not printed any object details like name, author or even book instance. We can see that there is no select statement fired in case of load

   

Now lets  update LoadBook.java to get the properties of objects

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

import com.tutorial.hibernate.Book;

public class LoadBook {

       public static void main(String a[])
       {

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

        System.out.println("Fetching Book with isbn number 3 ");
        System.out.println();
        Book book = (Book)session.load(Book.class, 3);
        System.out.println();
        System.out.println("Book Name =" + book.getName() );
        System.out.println("Book Author =" + book.getAuthor() );

        System.out.println();
        System.out.println("Fetching Book with isbn number 10 ");
        System.out.println();
        book = (Book)session.load(Book.class, 10);
        System.out.println();
        System.out.println(book.getIsbn());

       factory.close();
    }
}

Now we can see that select statement is fired because we have accessed book’s properties. Even if we print System.out.println(book), query will be fired.

For ISBN=10, we just accessed getIsbn() and we were able to get the value even if it does not exist because load always returns proxy object. Also to access the identifier, there is no select statement fired.

Lets access other property of object with ISBN=10 by calling getName() we can see that select statement is fired and ObjectNotFoundException is raised

 

 

Like us on Facebook