Difference between Hibernate get and load method

(242 Views)


Both get() and load() methods reside inside Hibernate's Session interface. Both do the same thing, to retrieve an object from the database. This object is nothing but a row from the table based on some query passed. The difference lies in the process of retrieval. Let's discuss each method in detail.

get() Method:

session.get() method is pretty straight-forward. We select a row from the table using an ID, and retrieve the data.

Lets say we have a table with roll number and name. Roll number is primary key, so we try to access the row based on its value.

Difference between Hibernate load() and get() method
int roll = 2; obj = (ExampleClass)session.get(ExampleClass, new Integer(roll)); String name = obj.getName(); System.out.println("Student with roll number "+roll+" is "+name); //Output //Student with roll number 2 is Krishna

Here's what happening inside:
As soon as we call session.get() method, it searches the database for the ID we passed. If it finds the desired row, it returns it, otherwise it returns NULL.
obj = (ExampleClass)session.get(ExampleClass.class, new Integer(12)); System.out.println(obj.getName()); //It will throw nullPointerException beacuse id 12 is not available in database.

load() Method:

When we call session.load() method, it doesn't search the table and returns the row directly, instead it returns a proxy object. Proxy object signifies that the object returned is not the real object containing table data. It is created in heap memory with an ID as passed.

When we try to access some value other than ID, then only the object searches the database and returns the data object(if present).

Here's a code snippet to clarify any doubts:

obj = (ExampleClass)session.load(ExampleClass.class, new Integer(2)); //obj is a fake object, it hasn't accessed the table yet String name = obj.getName(); //Now obj hits the table and returns the name with ID 2 System.out.println("Student with roll 2 is "+name); //Output //Student with roll 2 is Krishna

To make things even more clearer, here's another code snippet:
obj = (ExampleClass)session.load(ExampleClass.class, new Integer(287)); // No error till this point as we haven't accessed the table String name = obj.getName(); // Now obj search the table and doesn't find ID 287, Now error is raised Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists:

Conclusion:

The difference between Hibernate get() and load() methods is in the return value when the identifier does not exist in database:

get() methodload() method
We will get nullPointerException if identifier is not present. We will get objectNotFoundException Exception if identifier is absent.
get() method provides data implementation of eager loading i.e It loads all data initially load() method provides data implementation of lazy loading i.e It loads data when it is actually needed.
get() method can be used in both attached(session is open) as well as detached(session is closed) state. load() method can be used only in attached mode, if we try to use load method in detached state LazyInitializationException is thrown.

Solution Worked 1 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search