1.

What is Java Persistence API (JPA)?

Answer»

Hibernate’s session class has 2 POPULAR methods get() and load() to access an OBJECT. These 2 methods are similar in the work they do but have very subtle differences. The differences are as follows: 

  1. If an object is not found based on the ID passed to it, the get() method will return a null whereas the load() method will throw an exception. It throws the ObjectNotFound exception. 
  2. When using load() method, information other than the id can be retrieved from the proxy whereas get() will always hit the database for any information.  
  3. If the developer is sure of the object’s existence, it is always preferable to use load() as it RETURNS the information faster and without contacting the database. If the object’s existence is doubtful, it is preferable to use get() as it will return a null if it cannot find the object but will be SLOWER if the object does exist as it will contact the database for information. 
  4. If an object is found, get() method will return a fully initialized object whereas the load() method will return the object with the Id but may not initialize the other properties of the object. If you require an object with just the Id to create a relationship, use the load()method 


Discussion

No Comment Found