Saturday, October 18, 2008

difference between getHibernateTemplate().load and getHibernateTemplate().get

the HibernateTemplate is basically a wrapper around the native Hibernate API.

load() just creates a proxy and does not hit the database while get() does.
With load you need to re-attach the owning session somehow, which is only possible by locking it to the saved object. You don't really want to do that if your using the OpenSessionInViewFilter / Interceptor for your views as you will end up with a session remaining locked open


get() will return null if an object is not found while load() will always return a non-null object which is a proxy. If the underlying object does not exist, the proxy will thrown ObjectNotFoundException.
load() should be used when you are sure that the object exits while get() when you're not.

No comments: