Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Get v/s Load method in Hibernate - Why two methods are provided?

Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There are two methods available in Hibernate to retrieve the objects from the database:

1. Get Method
2. Load Method

Get method returns null when object is not present in the database and Load method throws an exception.
I am wondering for:

1. What is the benefit of defining these two methods?

2. We could have just one method instead of two and would have handled the scenarios accordingly, so, what is the benefit actucally we are getting by defining two methods?

Vaibhav Garg
Posts: 9
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will return a fully loaded object of the User entity with userId = 1 if found. it returns null otherwise

will return a proxy User object with the identifier property populated without hitting the database. It hits the DB only when you attempt to access a non-identifier property and it is then that it throws an exception if a record with userId = 1 is not found. Does not throw an exception while you are accessing the identifier property only.

Now, given a scenario where in a e-commerce system User is creating a Bid. Bid has a one-to-one association with User. When a Bid object is created, its user attribute needs to be populated with a User object. When the Bid object is subsequently saved in the Bid table the foreign key reference to User table needs to be set. Given this, you may want to ask yourself ; do you really want a fully loaded User object in Bid just for setting the value of a foreign key in Bid table OR is it more than sufficient to achieve the same thing with a proxy.

I'm full of tinier men! And a tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic