开发者

Load vs Get in Nhibernate

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 n开发者_如何学运维eeded 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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜