Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding session.get vs session.load method in hibernate

I am not able to understand the difference between load and get . the following piece of code doesn't work when i give session.load. It gives null pointer exception. But same does work when i am using session.get() .

public Employee getEmployee(final String id){        
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(Session session) 
                throws HibernateException,SQLException {
                //return (Employee)session.load(Employee.class, id);   doesn't work
                  return (Employee)session.get(Employee.class, id);    //it works
            }
        };        
        return (Employee)hibernateTemplate.execute(callback);
    }

I also want to understand how Session object is passed to doInHibernate.?
when does session starts and when it ends?

Stack trace is as follows

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
    at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3121)
    at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
    at org.hibernate.impl.SessionImpl.load(SessionImpl.java:781)
    at org.hibernate.impl.SessionImpl.load(SessionImpl.java:774)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1282)
    at $Proxy0.load(Unknown Source)
    at hibernate.EmployeeDao$1.doInHibernate(EmployeeDao.java:25)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
    at hibernate.EmployeeDao.getEmployee(EmployeeDao.java:29)
    at hibernate.SpringHibernateTest.main(SpringHibernateTest.java:26)
like image 700
Anupam Gupta Avatar asked Aug 03 '11 10:08

Anupam Gupta


People also ask

Which is Better get or load in hibernate?

The get() method fetches data as soon as it's executed while the load() method returns a proxy object and fetches only data when object properties is required. So that the load() method gets better performance because it support lazy loading.

What is Session get method in hibernate?

Hibernate Session provide different methods to fetch data from database. Two of them are – get() and load(). get() returns the object by fetching it from database or from hibernate cache. when we use get() to retrieve data that doesn't exists, it returns null, because it try to load the data as soon as it's called.

How load method works 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 is the default return type of session get () method?

get() It will always return null , if the identity value is not found in database.


1 Answers

I am not able to understand the difference between load and get 

The main difference is: if load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found. Other difference is that the load() method may return a proxy instead of a real instance but get() never does return proxy.

the following piece of code doesn't work when i give session.load. It gives null pointer exception. But same does work when i am using session.get() .

If object is not found, load method will throw exception but get won't.Simple

Edit: To elaborate the things,

  1. When get() method is called, it will directly hit the database, fetch the result and return. If no matching fields are found, it will gladly return null.

  2. But when load() executes, firstly it will search the cache for required object. If found, all is well. But if object is not found in cache, load() method will return a proxy. You can consider this proxy as a shortcut for database query execution. Remember, no database hit is made yet. Now when you actually access the object the proxy will be traced and database hit will be made.

Lets consider a simple example.

User user=(User)session.load(User.class, new Long(1));//Line 1
System.out.println(user.getPassword());//Line 2

If User object with primary key 1 is not available in session, the load() method will set a proxy for the database at Line 1. Now when actual value of the 'user' object is called, i.e. line 2, the proxy will be traced and the database will be hit.

Hope this will help.

like image 106
santosh-patil Avatar answered Sep 30 '22 07:09

santosh-patil