Wednesday, March 01, 2006

Simple difference between Hibernate get() and load()

get() and load() both...

Return the persistent instance of the given entity class with the given identifier

but get()...

[returns] null if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)

and load()...

assumes that the instance exists.

therefore...

You should not use this method [load()] to determine if an instance exists (use get() instead). Use load() only to retrieve an instance that you assume exists, where non-existence would be an actual error.

Links:
HIbernate 3 Session API (org.hibernate.Session)
Session.get()
Session.load()

No comments: