Hello friends,

Welcome back, today I am going to discuss the very important topic of hibernate as I promised. So let’s discuss get() and load() method of hibernate.

There is not much difference in code while working with either load() or get() method, But there are some minor difference between both method That helps you to handle get() and load() method problems so let us start the session:-

load() method

  • It is used when you sure that object exists in the database.
  • It will throw an exception of ObjectNotFoundException if the unique id is not found in the database.
  • It returns a proxy object by default.
  • It does not hit the database.
  • It performs by default lazy loading. I have already define lazy loading in the previous session. if you don’t know what is lazy loading so please click here
  • Let’s look at different types of load() method available in hibernate session:public Object load(Class theClass, Serializable id) throws HibernateException
    public Object load(String entityName, Serializable id) throws HibernateException
    public void load(Object object, Serializable id) throws HibernateException

Let see Example of the session.load():-

 

get() Method

  • If you are not sure that the object exists or not, then use one of the get() methods.
  • It will return null if the unique id is not found in the database.
  • It will hit the database immediately.
  • It returns the real object, not proxy object.
  • Let’s look at different type of load() method available in hibernate session:
    public Object get(Class className, Serializable id) throws HibernateException
    public Object get(String entityName, Serializable id) throws HibernateException

 

Let see Example of the session.get() :-

Now, I hope that all of you able to easily understood that difference between get() and load() method.

That’s all for this beginner level tutorial. In coming post, we will discuss more concepts around hibernate tutorial.

Happy Learning !!