Load vs Get in Nhibernate

2.9k views Asked by At

The master page in my web application does authentication and loads up the user entity using a Get.

After this whenever the user object is needed by the usercontrols or any other class I do a Load.

Normally nhibernate is supposed to load the object from cache or return the persistent loaded object whenever Load of called. But this is not the behavior shown by my web application. NHprof always shows the sql whenever Load is called. How do I verify the correct behavior of Load?

I use the S#arp architecture framework.

2

There are 2 answers

0
dmonlord On BEST ANSWER

If you use Get, then a hit to a database is made. If you use Load, no hit to a database is made, but the object (User in your case) is created with 'lazy loading'. So when you check a property it knows that you want data so it hits the database with a query to get the data.

If you want to get an object from cache, you need to consider 2 options. First level cache, is a cache that is in use in ONE session. So when you close a session or load the same object in a different session, you get additional hits. Second level cache works accross all sessions. If one session gets the object, the other session gets it from cache.

So what you want is probably a second level cache.

0
Ricardo Peres On

Actually, calling Load on an entity not marked as lazy causes immediate loading. That is because non-lazy entities are never proxied. In this case, it just acts the same way as Get.