• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use find() vs load ?

 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Hibernate has methods for finding - find() and loading load().
In my opinion, correct me if i am wrong, both do the same task.
Find that record and then return it.
I am unclear when we should use the find() method versus the load() method.

Thanks,
Gayatri
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a few of the differences, read the Hibernate reference for more details.

To use load() you need to know the object's identifier. If the object doesn't exist, load() throws an exception. load() returns a single entity.

get() takes an identifier and returns a single entity. It returns null if an object with that identifier is not found.

find() takes an HQL query instead of an identifier and returns a list of entities. (I think find() is deprecated in Hibernate 3.)

list() is similar to find() except it accepts parameters using a QueryParameters object instead of arrays and can accept a Criteria object as well as a String with HQL.
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scott.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic