3

I know that this is a common question that was described a lot of times. But still I can't get some thing - what about performance? Which method is faster?

Actually, I need a simple thing: the page makes a POST-request: id=0, name='John'. The server-side script should check if id=0, then create a new record in DB, otherwise update existing.

I think that get() is more usefull for me because it will return a NULL in case of record is not exists in DB, while load() can return some temproray object.. Am I right? Thank you

nKognito
  • 6,297
  • 17
  • 77
  • 138
  • it will depend on lots of things. Probably most the underlying database. Also what's the ratio of found/not found? From your formulation it sounds as if you expect a lot of "misses". – vidstige Oct 25 '11 at 09:09
  • found/not found means that record with such id actually exists in DB. Let's say I need some unique way to check if the record exists in DB or not (by load() or get() methods), but not at the expense of performance – nKognito Oct 25 '11 at 09:11
  • This description is quite clear http://gmarwaha.blogspot.com/2007/01/hibernate-difference-between-sessions.html – Damian Leszczyński - Vash Oct 25 '11 at 09:14
  • yes, but what is the *ratio*, ie what is the probability of a "not fonund" vs "found". – vidstige Oct 25 '11 at 09:14
  • The cheap solution is to execute count query against the table where you use in condition the id column. If you have large object to retrieve only to check that is in data base this might help. – Damian Leszczyński - Vash Oct 25 '11 at 09:17
  • @vidstige, the probability will be always 50%, the proportion from not "not found" to "found". – Damian Leszczyński - Vash Oct 25 '11 at 09:19
  • @Vash, the cheapest solution is to run count query? Are you sure? I think that get() method and result != null check is more cheap. Don't you think? – nKognito Oct 25 '11 at 09:39
  • I am sure, if you have a prepared statement, or criteria with proper projection and where clause, the execution time of statement will be lot faster. The primary key are in general clustered indexes, then while you are running get or load, in case the object is not in cache it have to be retrieved that often require join operation etc. – Damian Leszczyński - Vash Oct 25 '11 at 14:03
  • better explanation found here:http://stackoverflow.com/questions/608947/hibernate-difference-between-session-get-and-session-load – didxga Jun 20 '12 at 09:03

1 Answers1

6

Here's a link to the equivalent question in the Hibernate forum which was the first result in my google query (Hibernate load vs get): https://forum.hibernate.org/viewtopic.php?p=2387456

There it states the following:

The load() method is older; get() was added to Hibernate’s API due to user request. The difference is trivial:

If load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • I saw this topic. The question is more like: is the load() method faster then get()? Or perhaps vice versa – nKognito Oct 25 '11 at 09:16
  • @nKognito Looking into the Hibernate 3.4 source there's not much difference. Actually the same methods are called with just a difference in the checks as stated above. – Thomas Oct 25 '11 at 09:20
  • 1
    So we can say that method get is faster because, there is no need of the block try catch for her. – Damian Leszczyński - Vash Oct 25 '11 at 09:26
  • 6
    You'll never notice the performance difference, because it'll be swamped by other features in your code: poor schema, missing indexes, inefficient object model, etc. Don't worry about nano-optimizations like this. – duffymo Oct 25 '11 at 09:38