这篇文章
http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id
One of the more common mistakes that I see people doing with NHibernate is related to how they are loading entities by the primary key. This is because there are important differences between the three options. The most common mistake that I see is using a query to load by id. in particular when using Linq for NHibernate. var customer = ( select customer from s.Linq<Customer>() where customer.Id = customerId select customer ).FirstOrDefault(); Every time that I see something like that, I wince a little inside. The reason for that is quite simple. This is doing a query by primary key. The key word here is a query. This means that we have to hit the database in order to get a result for this query. Unless you are using the query cache (which by default you won’t), this force a query on the database, bypassing both the first level identity map and the second level cache. Get and Load are here for a reason, they provide a way to get an entity by primary key. That is important for several aspects, most importantly, it means that NHibernate can apply quite a few optimizations for this process.
好像在说,不要用主键(by primary key)去查询,因为用主键区查询会绕开一级和二级缓存。
所以建议用Get和Load去查询
下面的测试是错误的,要看正确的盖棺定论,请跳转至:
终极测试:
--------------------------------------------
实现延迟加载:
如何实现延迟加载:
要在实现延迟加载的类的映射文件xxx.hbm.xml中添加 lazy="true",根据加的位置,有两种:
一是类级别的:
<class name="Model.Customer, Model" table="Customer" discriminator-value="0" lazy="true"> <!--unsaved-value="0" 主键表中不需要定义,而是需要在子表中定义--> <id name="CustomerId" column="CustomerId" type="int" unsaved-value="0"> <generator class="native" />
。。。。
二是集合级别的
<class name="Model.Customer, Model" table="Customer" discriminator-value="0"> 。。。。。 <set name="Orders" table="Order" lazy="true" generic="true" inverse="true" cascade="all"> <key column="CustomerId" foreign-key="FK_CustomerOrders"/> <one-to-many class="Model.Order,Model"/> </set> 。。。
根据目前的测试,集合位置的延迟加载配置会覆盖类级别的延迟加载配置。
有两种获取实体的方法:Get和Load方法。测试了如下组合:
类级别的lazy="true" 且 类级别的lazy="true"且
集合级别的lazy="true" 集合级别的lazy="false"
--------------------------------------------------------------------------
Get方法 | 加载父,延迟加载子 父子都非延迟加载
Load方法 | 父子都非延迟加载 父子都非延迟加载
从上面可以看到:
Load方法是不理会lazy="true" 配置的,永远的是非延迟加载。用Lode方法获取Customer时,Customer和子集合Order永远都是立即加载(不管哪里配置了lazy =“true”)。
刚好和:
http://blog.knowsky.com/192505.htm
描述的完全相反。到底谁对谁错,纠结啊
通过一夜的调试:
发现,如果是设断点去调试,那么Load表现为非延迟加载,发送了SQL语句,
这可能是编译器的问题。上面说的
刚好和:
http://blog.knowsky.com/192505.htm
描述的完全相反。到底谁对谁错,纠结啊
应该可以更正回来了,
类级别的lazy="true" 且 类级别的lazy="true"且
集合级别的lazy="true" 集合级别的lazy="false"
--------------------------------------------------------------------------
Get方法 | 加载父,延迟加载子 父子都非延迟加载
Load方法 | 父子都延迟加载 父子都非延迟加载
最终的结论是:
在配置lazy = “true”的情况下,
Get方法仅延迟加载子集合Orders,
Load方法比Get更牛逼,不仅延迟加载子集合Orders,把父Customer也延迟加载了。
Load方法是假设父Customer是存在,若不存在,会引发异常“NHibernate.ObjectNotFoundException”类型的异常,
但是这种异常,Nhibernate自己内部处理了,并返回一个实体类的代理(CustmoeProxy),保证不返回一个null
如下图所示:
------------------------------------------
非延迟加载图解:
延迟加载图解:
看到,当实现延迟加载,当需要Orders时,会通过session去查询,session。但此时,由于session已经关闭,
所以会抛出异常:
{"Initializing[Model.Customer#336]-failed to lazily initialize a collection of role: Model.Customer.Orders, no session or session was closed"}
为了避免session被关闭,延迟加载失败。有如下解决方案:
public Orders LazyLoadOrders(int customerId) { using (ISession _session = new SessionManager().GetSession()) { return _session.Get<Customer>(customerId).Orders; } }
以后就调用这个方法来获取Orders
--------------------------------------------------------------------------------------------------------------
终极测试:
注意:测试时不要打断点,否则会影响测试结果(如果打了断点调试,会有多余的SQl请求产生),上面的测试就是因为打了断点去测试,结果
影响了测试结论。
测试的最终结论,上面所以的测试结论以下面的测试结果为盖棺定论。
下面的<class>和<Set>都是属于Customer.hbm.xml中的设置。
【1】Get方法:
<class name ="Customer" lazy="true" > <class name ="Customer" lazy="false" >
<set name="Orders" lazy="true"> 加载Customer,不加载Orders 加载Customer,不加载Orders
<set name="Orders" lazy="false"> 加载Customer, 加载Orders 加载Customer, 加载Orders
结论:1.Get方法永远加载Customer;
2.Get方法是否加载Orders,由<set name="Orders" lazy="">决定。
【2】Load方法:
<class name ="Customer" lazy="true" > <class name ="Customer" lazy="false" >
<set name="Orders" lazy="true"> Customer和Orders都不加载 加载Customer,不加载Orders
<set name="Orders" lazy="false"> Customer和Orders都不加载 加载Customer,加载Orders
结论:1.Load方法只要Class的Lazy为false,永远都不加载Customer和Orders;
2.Load方法只要Class的Lazy为ture,永远加载Customer,是否加载Oders由<set name="Orders" lazy="">决定。
01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响
原文:http://www.cnblogs.com/easy5weikai/p/3754263.html