Tuesday 8 September 2009

The difference between NHibernate Get and Load

If no row with the given identifier exists in the database the Get() or Get<>() method returns null.
In the case of Load() or Load<>() if the object can't be found by the given identifier an exception is thrown. Load() or Load<>()never returns null.
If you are using lazy loading the Load() or Load<>()may return a proxy instead of a real persistent instance.
Get() or Get<>() never returns a proxy because it must return
null if an entity with the given identifier doesn’t exist.
Use Load() or Load<>() if you’re certain the persistent object exists and nonexistence would be considered an error. If you aren’t certain that the entity exists use Get() or Get<>() and test for null.