I am not able to understand the difference between load and get
The main difference: if load () cannot find the object in the cache or database, an exception is thrown. The load () method never returns null. Returns the get () method if the object is not found. Another difference is that the load () method can return a proxy instead of a real instance, but get () never returns a proxy.
the following piece of code doesn't work when i give session.load. It gives null pointer exception. But same does work when i am using session.get() .
If the object is not found, the load method will throw an exception but not get it. Plain
Edit: To clarify,
When the get () method is called, it immediately enters the database, retrieves the result, and returns. If no matching fields are found, it will happily return null.
But when load () is executed, firstly, it will look for a cache for the required object. If found, all is well. But if the object is not found in the cache, the load () method returns a proxy. This proxy can be seen as a shortcut for querying the database. Remember that no database hits yet. Now that you are actually accessing the object, the proxy server will be tracked and the database will be hit.
Consider a simple example.
User user=(User)session.load(User.class, new Long(1));//Line 1 System.out.println(user.getPassword());//Line 2
If the user object with primary key 1 is not available in the session, the load () method will set the proxy for the database on line 1. Now that the actual value of the user object is called, that is, line 2, the proxy will be traced and the database will be deleted .
Hope this helps.
source share