Get all updates of Java4s
Follow Us: Email Newsletter

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

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()




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()



  • 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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


91 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.

  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

  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.


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful LinksCurrent & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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