top of page
index-1-1.jpg

Get and Load Methods in Hibernate

Both Get and Load methods in hibernate fetch row from databases. The functionality is similar but there is a difference between the way they work.

hibernateSession.get()

This method will return the real Object from the database of the mapping class.

hibernateSession.load()

This method returns persistent instance of mapping entity assuming that required object is in database. The object is not initialized with the required row in database when load is called. Instead Initialization occurs when we try to access the object property.Basically it returns a Proxy, a hibernate term.


Examples by Codes Let say we have a product class mapping to product table in database. The following code will show following output when get() method is called. Make sure that you have set

<property name="show_sql">true</property>
       	<property name="hibernate.show_sql">true</property>

in your hibernate.cfg.xml.

get()

product = (Product) session.get(Product.class, productId);
System.out.println("Query printed or executed when get() is called");

Console output Hibernate: select product0_.id as id1_0_0_, product0_.code as code2_0_0_, product0_.name as name3_0_0_, product0_.price as price4_0_0_ from product product0_ where product0_.id=? Query printed or executed when get() is called

As you can see that both queries are executed when get is called.

load()

Now we will use load method instead of get.

product = (Product) session.load(Product.class, productId);
System.out.println("Query is not printed or excuted when load() is called");
System.out.println("Query will now be printed or excuted when property of instance is called");

assertEquals("COKE",product.getName());

System.out.println("Query printed or executed when we tries to get property of instance. In this case it is name");

Console output Query is not printed or executed when load() is called Hibernate: select product0_.id as id1_0_0_, product0_.code as code2_0_0_, product0_.name as name3_0_0_, product0_.price as price4_0_0_ from product product0_ where product0_.id=? Query printed or executed when we tries to get property of instance. In this case it is name.

Let say that now we want to retrieve an object which is not in database. Let us see how these methods behave.

get()

product = (Product) session.get(Product.class, 100);

        if(product==null){
            System.out.println("System will Through NPE as object is not found in database");
            throw new NullPointerException();
        }

Console output Hibernate: select product0_.id as id1_0_0_, product0_.code as code2_0_0_, product0_.name as name3_0_0_, product0_.price as price4_0_0_ from product product0_ where product0_.id=?

System will Through NPE as object is not found in database.


As you can see query is executed and null is returned by get method when object is not found.


Now let us look at load method and see how it behaves

load()

product = (Product) session.load(Product.class, 100);

        if(product==null){
            System.out.println("System will not throw NPE as a Object/Proxy is returned from database");
        }

        System.out.println("System will throw ObjectNotFoundException when I try to get property of the instance");
        product.getName();

Console output System will throw ObjectNotFoundException when I try to get property of the instance Hibernate: select product0_.id as id1_0_0_, product0_.code as code2_0_0_, product0_.name as name3_0_0_, product0_.price as price4_0_0_ from product product0_ where product0_.id=?


As you can see that test case didn’t throw Null Pointer Exception instead it just simply returned instance of required type. But when we try to get instance property it will throw ObjectNotFoundException and say that no row is found for given identifier.


Personally I prefer to use load() method as it is less expensive in term of performance but it requires lot of attention as it return instance instead of null so we have to be careful when we use load() method.


Hope this article will help you in learning more about Hibernate. Please feel free to add your comments, recommendation and questions. I will try to follow them as much as possible.


32 views0 comments

Recent Posts

See All

TRANSIENT FIELD WITH ANNOTATION VALIDATORS

Recently I got the chance to work with annotation validators @Pattern on transient field. Basically it was a password field which I had to validate first and then encrypt it and set it in another pers

Many To Many Relationship Using Annotations

This article is about how to write many to many relationships using annotations in hibernate. Many to many means that we can have many rows in one table against many rows in another table. Here is an

Many To Many Relationship Using XML

This article is about how to write many to many relationship using XML in hibernate. Many to many means that we can have many rows in one table against many rows in another table. In my previous artic

bottom of page