Difference Between Hibernate get() and load() Methods ?

Hibernate » on Sep 18, 2014 { 116 Comments } By Sivateja

What is the difference between hibernate get() and load() methods ? this is one of the famous hibernate interview questions,ย  most of us will use hibernate get and load methods irrespective of knowing their exact behavior ๐Ÿ™‚ will you accept that ? of course even myself too in the initial stages ๐Ÿ™‚ Today i will try to clear that confusion.

Few Points About Hibernate get() & load()

  • Both are from Session interface, and we will call them as session.get() & session.load()
  • Both will be use for retrieving the object (a row) from the database

Then what’s the difference them ? lets start with load() and then get() method.

Consider a Student class having 3 properties stdId, stdName, stdCountry

1. session.load()

  • When you call session.load() method, it will always return a “proxy” object,ย  whats the meaning of proxy object ?
  • Proxy means, hibernate will prepare some fake object with given identifier value in the memory without hitting the database, for example if we call session.load(Student.class,new Integer(107));ย  > hibernate will create one fake Student object [row] in the memory with id 107, but remaining properties of Student class will not even be initialized, observe this graphical representation…
  • It will hit the database only when we try to retrieve the other properties of Student object i mean stdName, stdCountry.ย  If we call s2.getStdName() then hibernate will hit the database and search the row with student id 107 and retrieve the values, if object [row] not found in the database it will throws ObjectNotFoundException.,

Let me explain each point by taking an example

Example of session.load()


System.out.println("Student is calling with load()");        
s2 =(Student) session.load(Student.class,new Integer(107));    
System.out.println("Student called with load()");
System.out.println("Printing Student Name___"+s2.getStdtId());        
System.out.println("Printing Student Name___"+s2.getStdName());



In index.jsp > line number 4,ย  i have called s2.getStdtId() and hibernate simply printed 107 [at Output > line number 3] without creating any database query why ? because as i have explained hibernate will prepare some fake object with given identifier value in the memory without hitting the database.ย  So when we call load() method (at index.jsp > line number 2 ) with 107 value, hibernate will create a fake object with 107 as identifier value, i mean temporarily it will consider the student id as 107.ย  If you observe the output, hibernate was generated the database query (at Output > line number 4 )ย  when we called s2.getStdName(); ( index.jsp > line number 5 ).

That’s it, so finally we came to know that session.load() will hit the database only when we start retrieving the object (row) values.

2. session.get()

  • When you call session.get() method, it will hit the database immediately and returns the original object
  • If the row is not available in the database, it returns null

Example of session.get()

System.out.println("Student is calling with get()");ย ย  ย ย ย  ย 
s1 = (Student) session.get(Student.class,new Integer(107));ย ย  ย 
System.out.println("Student called with get()");
System.out.println("Printing Student Name___"+s1.getStdtId());ย ย  ย ย ย  ย 
System.out.println("Printing Student Name___"+s1.getStdName());



  • Nothing to explain here ๐Ÿ™‚ as i told, when we call session.get() method hibernate will hit the database and returns the original object [ row ], that’s the reason it was generated a query [ check line number 2 in the output screen]

Hmm…, so which is the best method to use, hibernate load() or get() ? its completely your choice ๐Ÿ˜‰

โ€‹ โ€‹โ€‹

You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


116 Responses to “Difference Between Hibernate get() and load() Methods ?”
  1. Poonguzhaly says:

    Hi Sivateja Kandula,
    Really simple explanation and easy to relate. Thank you very much.

  2. ramakrishna says:

    Very good explantion…

  3. Nandu says:

    Best explanation ever…

  4. Ram R Tiwari says:

    Hi Sivateja,

    Really very good explanation. Keep growing ๐Ÿ™‚

  5. rajesh says:


    Article is very very good.

    sorry to say in this way: I know the difference as per my text book but in real time in which scenario we have to use load/get till not getting and not assuming also. if you give a simple example , that can be remembered more easily.

    • Shiva says:

      If u are not sure that data which is you are searching for is present in dB then u go for load() else get()

  6. Raj Kishor Singh says:

    Excellent explanation

  7. Raj Kishor Singh says:

    keep the same momentum

  8. Nagarajan P says:

    first of all i thank you very much. your explanations are very nice. it’s easy to understand. My doubt is, get() returns nullpointerexception if the data is not there in the database. you told it returns null. i’m totally confused, can you give a clarification?

    • Naga says:

      Nagarajan: get methode return 'NULL' value when we call Student st=session.get(); if no row exist in the database.

      after the if we call st.getId() here it will throw Null Poointer Exception

      • Rishabh says:

        s1 = (Student) session.get(Student.class,new Integer(107)); // here if s1 value is null. So if you will call any thing with null object obviously it will throw null pointer exception

  9. Pramod says:

    Can we conclude, that load() is used for lazy loading and get() is used for early loading?

  10. Neeraj Kashyap says:

    Indeed awesome explanation ๐Ÿ™‚

  11. SAI KRISHNA says:

    If possible can u please provide JSF tutorial also.

  12. dbin says:

    totally disagree load() may hit the database if lazy=false i.e in eagar loading so it not always return proxy object

  13. Ananta says:

    Hi fnd,

    Really very good explanation.if u try to upload some this type of explanation in other tutorial like Core java,its very helpfull to all .

  14. Rajashekar-PearlPoint says:

    Hey Dear,

    Its really amazing to understand. I am big fan of you and your posts, I my self improved a lot in java following your blog. Keep it up ๐Ÿ™‚

  15. Brijesh Rana says:

    Thank you, sir for this awesome explanation

  16. Sailaja says:

    Your explanation too gud.. keep up the gud work !!!

  17. Brahmaiah says:

    This hibernate tutorial is very good, I had few queries/doubts before reading this tutorial, but now i am very clear. Thanks a lottttt.

  18. Navneet says:

    Hi Sivateja,

    Excellent explanation

  19. Deepesh says:

    Nice Explanation in easy to understand way.. Colorful text makes it pretty clear in terms of readability.

    Thanks !

  20. Satya says:

    Each and every word makes sense. Nice explanation this would help to others very much. Easily understandable…… I think all get benefited by u….

    Thanks !

  21. smruti says:


  22. Chetan says:

    Nice Explanation…….Thanks

  23. Srinivasa Reddy Challa says:

    Very Good Explanation really helpful.

  24. Satya Adabala says:

    Very good explanation..Thanks

  25. Ranjana says:

    Really its a great explanation.

  26. Rcr says:

    There is more to this ( than being an interview question )
    As per Hibernate in action book :

    The load() method is older; get() was added to Hibernateโ€™s API due to user
    request. The difference is trivial:

    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.

    Always use get()

  27. Souvik says:


    Very good and lucid explanation, the very best i have read until now, keep up the good work, all the best!!

  28. rahulsahow says:


    According to my perception, your explanation is very easy to understand not only me everybody can understand, i hope

  29. Sagar says:

    Best explanation…thanks

  30. shailesh says:

    Very nice explained

  31. Lakhan says:

    Thanks buddy !!! If u know pls tell us how merge method works in hibernate.

  32. Niraj Sachan says:

    Good explanation…just want to add on load() method….
    Session.load() will use (lazy / eager) loading depends on implementation of persistent class – Final / Non Final.

    1. If persistent class is non final then hibernate build proxy of class in cache and use further lazy loading to get others class properties by hitting db.
    2. If class is final then hibernate cannot build proxy of class and use eager loading by hitting database.

  33. milind says:

    Good explanation thanks……

  34. Ravi Sankar says:

    Thanks for the elaborated explanation

  35. surya says:

    thanks alot Sivateja Kandula for your well designed material for beginners.learnt alot !!!!

  36. Srushti says:

    Thanks a lot Sir, this is very clear explanation. Everytime when I tried to understand the difference between load() and get(), I never understood what the proxy is. But you have explained it very well.

  37. sanjib says:

    Really Good to understand .Thanks

  38. really very good explaination.

  39. Ajit Kumar says:

    Thanks very much for ur explanation ….

  40. shwetha says:

    simple and sweet.. ๐Ÿ™‚ nice explanation

  41. sangrambadi says:

    its nice explanation…..i understood clearly…..but my doubt is…why
    two method…what is the best…why load returns…proxy…when to use what.. advance…

  42. abdulla says:

    nice explanation sir

  43. KS says:

    Very nice explanation..Thank you very much!!

  44. Atul Singh Chauhan says:

    Nice explanation


  45. santosh says:

    Can you please share difference between lazy loading and eager loading with example and well explanation?

  46. Buchireddy says:

    Nice explanation


  47. Pravesh Yadav says:

    Very Good Explanation,,, Thanks

  48. Raju says:

    Really very good explanation….

  49. bhumi says:

    It really very good explaination. its really become simple to understand. ๐Ÿ™‚

  50. Ingnam says:

    thank you Sivateja, Simple but best explanation ….

  51. venkat says:

    very nice explanation

  52. Venkatakumar says:

    Very good explanation and easier to understand. i didn’t get this much clarity from other popular sites..Thanks

  53. Aaftab says:

    Very good tutorial.Thanks

  54. Harinadh says:

    Very good explanation..

  55. mahaboobmastan says:

    hi sir nice website but i need as spring to hibernate integration examples
    and provide some real time examples based on hibernate
    thank u sir

  56. Ravi Smile says:

    I have hands on hibernate earlier but after reading your java4s tutorial i got better idea. Good Job keep it up:)

  57. Arpit says:

    Great one

  58. Swapnil N says:

    Nicely explained the difference between get() and load() method.Thanks a lot for sharing such useful information.

    Here,I would like to add one more point regarding when we can go for get() and load() method.If you agree upon it,please go ahead and implement in your application,else ignore it.

    load() – since, it returns a proxy object of requested entity,we can use it for lazy initialization purpose,where we need only a reference of an object and no other its properties.It actually saves DB hits.
    one precaution we have to take is that we should use this method only when the data is present in the DB ,else it throws the exception.

    get() – we should go with this method when we want to make sure the required object i.e. data is present in the DB.

    Thank you.

  59. Vishal says:

    Explain different method of loading ? It is question of insurance exam final years. Please help

  60. Shweta says:

    ThankU…Nice explanation..!!

  61. hemant says:

    nice example

  62. vrk says:

    Thank u. very nice explanation. grt job

  63. Manzoor says:

    Very good explaination:) Way to go !!!

  64. Prince Singh says:

    Thank’s Nice explaing

  65. Prashant says:

    Very clear explanation

  66. chinni says:


  67. Nirmalya Roy Karmakar says:

    Best Explanation

  68. arpit says:

    you are awesome bro. i never see such kind of explanation which so much clarity.Thanku so much

  69. kanchan says:

    Really good explanation,after reding this not only me but no any one having problem in get and load method

  70. Ritz says:

    Thank you very much for these good tutorials.
    I was scared of Hibernate before but now I feel its easy. ๐Ÿ™‚

    Thank you Java4s Team.

  71. Alok says:

    Thank u For this tutorial, and Very Good explain.

  72. Nitin says:

    really its a very very good explanation….

  73. moula ali shaik says:

    simply superb explanation keep going

  74. PANKAJ MANGAL says:

    Only one word "awesome"

  75. pawan says:

    really explained very simply and can be understand easily.

  76. aman verma says:

    please explain more about load() and get() for annotation..

  77. Ravi Chaurasia says:

    really explained good and never seen this type of explaination

  78. kaml says:

    Great Tutorial.Awesome ๐Ÿ™‚

  79. sapna says:

    Very nicely Explained, Thanks sir.

  80. Pradeep ch says:

    Could you please explain about locking in hibernate.

  81. SUDHEER says:

    how to create proxy object in session.load().method,can u explain the internal proces?

  82. Khairunisa says:

    Hello Sir,Your Explanation is super with graphical representation and with simple examples,Thank you Sir.

  83. Sasmita says:

    Super Explanation,Thank you much

  84. Sonu says:

    Hii ,
    I am getting an error called " org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] ".

  85. Priya says:

    Clear Explanation , Easy to understand thanks for sharing.

  86. Bo says:

    Finally, i have completed this tutorial.
    Your tutorial is very easy to follow it.
    Thank you.

  87. Ritu says:

    That is an awesome explanation. Thanks Man!! ๐Ÿ™‚

  88. Ram says:

    its awesome explanation…..everyone can understand very easily…..thnx

  89. Sree P says:

    Excellent and Awesome explanation.

  90. Manisha says:

    You said —- so which is the best method to use, hibernate load() or get() ? its completely your choice.

    I am not satisfied.. I think load() method is used whenever you are sure about the existence of an object otherwise get() method is used cause when you call load()method with 101 id which is not exist in db then hibernate returns you as proxy object with 101 id i.e not it choose whenever you are sure about exist of data.

    • jaswanth says:

      How can you predict whether the data is available in the DB?

      • Manisha says:

        I cant predict about the existence of data but i want to say that there is no means of load methods because if there is no data in db it return proxy id 101.
        So it is always choose to get methods right.

        • jaswanth says:

          I agree, but if you choose get, lets say object is not available in the database, it will return NULL and further if we do any operations on NULL it will throw NullPointerException, then both Load and Get doing the same!

  91. Test says:

    Excellent explanation…I was searching better answer and here I could see moreover no question to ask after reading this topic…keep it up…

  92. Anil says:

    Came here for a better though. I found basic one. Not bad.

  93. Mohan says:

    Really its a great explanation.

  94. Monika says:

    Awesome. Now I got it. always wondering when load exception is thrown without checking identifier. how it created proxy object etc. U rock Man!!

  95. maha says:

    Really its a great explanation.


  96. Adriana Coanda says:

    Awesome explanation.
    I'm waiting for other interesting topics

    Thank you

  97. Siva T says:

    Yes, Really it's great explanation

  98. piyush says:

    imp interview question
    Get():-Eagle Loading
    Load():- Lazy loading

  99. Mounika Reddy says:

    Your way of explanation is awesome. Very easy and clear to understand the concept and especially with this kind of explanation its very easy to remember the things in memory.

  100. srinu says:

    hai i am using hibernate 4.3.5 jar,

    it will always return a โ€œproxyโ€ object whit out hit db;

    Session session = factory.openSession();
    Product product = (Product) session.load(Product.class, 105);

    Hibernate: select product0_.p_id as p_id1_0_0_, product0_.price as price2_0_0_, product0_.p_name as p_name3_0_0_ from product product0_ where product0_.p_id=?

    i am print id .hit db;

  101. vineela says:

    Very simple and clear explanation. Saved so much of time.

  102. Sivakumar says:

    Hi Sivateja, Usually, I am not commanding, but this website gives extraordinary explanation hats off man.
    Keep Rocking..

  103. Parshi Bharath says:

    Very Good Explanation Thank you very much

  104. Nagesh says:

    Good Explaination thanks


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.