What is difference between Hibernate Session get() and load() method?

session.load()

• It will always return a “proxy” in Hibernate terms without hitting the database. In Hibernate, proxy represents a fake object with the given identifier value but its properties are not initialized yet.
• If no record found, it will throws an ObjectNotFoundException.
• Example: Let we call session.load(Employee.class,new Integer(10)). Hibernate will create one fake Employee object [row] in the memory with id 10, but other properties of Employee class will not even be initialized. When we try to retrieve other properties of Employee object, it will hit the database and search the record with employee id 10 and fetch the values. It will throws ObjectNotFoundException in case when no record found.

session.get()

• It always hit the database and return the real object, an object that represent the database record not proxy.
• It return null if no record found.
• It will not throw any exception in case when no record found.

No comments: