try {
x = session.load(...);
y = x;
}
catch(Exception e) {
//do something
}
If the key is not present, will load(…) throw an exception
- right away
- will return a proxy and then throw an exception
when the object is trying to be obtained from the db later?
Also, in the code above, if the execution reaches the assignment y = x, is it guaranteed that at that point, x is not null? Are there situations where x can be null at that point?
In short:
xwill not be null after the load, so neither willyload()if the object doesn’t existload(), and the object doesn’t exist, there will be an exception when accessingxory.To elaborate:
Are you trying to determine if the key is present? Or are you assuming it is, but just want to handle anomalies?
See the documentation, specifically:
From the book ‘hibernate in action’ on using load():
So:
If you are looking to know whether the item exists, use
get(), notload().