I was under the impression that session.load()
loads the proxy object in the cache while session.get()
always hits the database but I am confused after watching a JavaBrains video.
According to this video when we are calling the below get method it is loading the proxy object of UserDetails
in the memory.
user = (UserDetails) session.get(UserDetails.class, 1);
Structure of UserDetails
is
While in the comment section, a guy commented:
there is no proxy of User class, instead the proxy object of the collection is created.
Now there are two questions here.
1st: Related to fetching strategies and creation of proxy objects in case of session.load() and session.get() which has been answered below by me.
2nd: In this case, the proxy object will create for UserDetails or for collection (still to be answered).
Thanks
In hibernate, get() and load() are two methods which is used to fetch data for the given identifier. They both belong to Hibernate session class. Get() method return null, If no row is available in the session cache or the database for the given identifier whereas load() method throws object not found exception.
Hibernate Session provide different methods to fetch data from database. Two of them are – get() and load(). get() returns the object by fetching it from database or from hibernate cache. when we use get() to retrieve data that doesn't exists, it returns null, because it try to load the data as soon as it's called.
load() It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object.
When ever the load() method is called, the hibernate creates a proxy object of a POJO class (provided as parameter), and it will set the id to the proxy object, then it returns the proxy object to the program.
1.Fetching Strategies: There is no effect of fetching strategies in the working of session.get or session.load (https://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch20.html#performance-fetching-lazy).
2. Session.get: It never returns proxy, As per the hibernate docs : (https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#get(java.lang.Class, java.io.Serializable))
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance. (If the instance is already associated with the session, return that instance. This method never returns an uninitialized instance.)
Means get
method first check the cache if the fully initialize object
is present there if yes return that object otherwise it hits the database to get the object
and returns the same after saving it in the cache space.
3. Session.load: As per the hibernate docs :
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.
Means load
method first check the cache if the fully initialize object
is present there and if yes returns that object else it returns the proxy(A proxy is a class that delegates to another object. Initially, when it's not initialized, it contains just the primary key. When you call a method, as the Javadoc says, it initializes, by loading the actual entity from the database, and the delegates to this loaded entity) of that object by `assuming that instance exists'.
Note: Important to note that load
method never throw an exception
.You will get ObjectNotFoundException
in case you try to retrieve any other property instead of the primary key from the proxy object.As it will hit the database to load the object from there, which is not exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With