Monday, January 18, 2010

Load() and Get() methods in NHibernate

For some time I have been using the Load() and Get() methods of NHibernate Session object without actually knowing the difference between the two methods. I kind of simply assumed the difference was architectural, but when I later ran into problems I had to examine the methods more in detail and discovered that the difference is pretty fundamental…

In short the Load() and Get() methods find objects based on it’s id (i.e. the primary key).  But what happens if the object is not found? The Load() methods throws an exception and the Get() methods returns a null object. That is quite a big difference! The NHibernate documentation is very accurate in that the Load() method should not be used if you are not sure if an object with the specific id exist, and hence it is a pretty big error if the object does not exist.

No comments:

Post a Comment