Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between get() and load() method of hibernate session with respect to fetching?

Tags:

java

hibernate

What is difference between get () and load() method? with respect to data fetching approach

 public static void main(String[] args) {
    SessionFactory factory= new Configuration().configure().buildSessionFactory();
     Session session = factory.openSession();
     Transaction tx = null;
      tx = session.beginTransaction();
       System.out.println("1 st time calling load method");
        Account acc = 
               (Account)session.load(Account.class, 180); 
             System.out.println("bal"+acc.getBalance());

          System.out.println("2nd   time calling load method");
          Account  acc1=(Account)session.load(Account.class, 180); 
           System.out.println("bal"+acc1.getBalance());


        System.out.println("1 st time calling get method");
     Account acc2= (Account) session.get(Account.class, accId);

      System.out.println("bal"+acc2.getBalance());    

      System.out.println("2 st time calling get method");

     Account  acc2= (Account) session.get(Account.class, accId);

    System.out.println("bal"+acc2.getBalance());


     tx.commit();

   session.close(); 

}

I got following output

1 st time calling load method
Hibernate: 
/* load com.abcd.Account */ select
    account0_.ACCOUNTID as ACCOUNTID1_0_,
    account0_.ACCOUNTTYPE as ACCOUNTT2_1_0_,
    account0_.CREATIONDATE as CREATION3_1_0_,
    account0_.BALANCE as BALANCE1_0_ 
from
    a.MYACCOUNT account0_ 
where
    account0_.ACCOUNTID=?
bal3000.0
2nd   time calling load method
bal3000.0
1 st time calling get method
bal3000.0
2 st time calling get method
bal3000.0

From ouput it is clear that get method did not hit database.It behaves like load() method. Could any one tell me is this behavior correct.

like image 897
sar Avatar asked Mar 03 '14 09:03

sar


People also ask

What is the difference between Save () and persist () method in hibernate?

The save() method provides an identifier with the intent of an insert query being executed immediately for getting the identifier. It does not matter whether it is outside or inside a transaction. The persist() method fails to execute a given insert query in case it is placed outside transaction boundaries.

How load () method works internally in hibernate?

When ever the load() method is called, the hibernate creates a proxy object of a POJO class (provided as parameter), and it will set the id to the proxy object, then it returns the proxy object to the program.

Which statement is correct about Session Load () in hibernate?

In session. load(), Hibernate will not hit the database (no select statement in output) to retrieve the Stock object, it will return a Stock proxy object – a fake object with given identify value. In this scenario, a proxy object is enough for to save a stock transaction record.


2 Answers

As T Mishra states here:

  1. By default, hibernate creates run-time proxies. It loads the objects as a proxy unless a fetch mode is specified or set to false.

  2. That's because once the object is loaded in cache, the next subsequent calls perform repeatable read.

  3. Although the state of this object changes from persistent to detached

The entity can be retrieved in 2 ways.

load() - returns the proxy object with an identifier.

get() - returns the complete object from database.

for more details click this link

like image 70
Engineer Avatar answered Oct 04 '22 02:10

Engineer


Actually, both functions are use to retrieve an object with different mechanism,

  1. session.load()

    It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. If no row found , it will throws an ObjectNotFoundException.

  2. session.get()

    It always hit the database and return the real object, an object that represent the database row, not proxy. If no row found , it return null.

like image 44
Ashish Chaurasia Avatar answered Oct 04 '22 02:10

Ashish Chaurasia