get() and load() are both Session methods for retrieving an entity by its identifier, but they differ in what happens when the row doesn't exist and in how eagerly the data is actually fetched.
Key Points: • get() hits the database immediately and returns null if no matching row is found. • load() returns a lazy proxy immediately without hitting the database, and only throws ObjectNotFoundException when the proxy is actually accessed and no row is found. • Because load() can return a proxy without a database call, it's more efficient when you only need a reference to set on another entity (e.g. a foreign key association) and don't need the actual data. • get() is safer when you genuinely need to check whether the entity exists, since a null check is simpler than catching an exception. • Both methods first check the first-level cache before hitting the database, so repeated calls for the same identifier in one session don't necessarily re-query.
Example: Calling session.get(Employee.class, 99) when no employee with id 99 exists returns null immediately, while session.load(Employee.class, 99) returns a proxy silently and only throws ObjectNotFoundException the moment you call a method like proxy.getName() on it.
Code Example:
Employee e1 = session.get(Employee.class, 99L); // returns null if missing
Employee e2 = session.load(Employee.class, 99L); // returns proxy, throws on access if missingInterview Tip: A concise interview answer is:
"get() immediately queries the database and returns null if the row isn't found, while load() returns a lazy proxy without querying and only throws ObjectNotFoundException when you actually access it. I use load() when I just need a reference for an association, and get() when I need to check existence."