Lazy Loading in NHibernate

試著忘記壹切 提交于 2020-01-11 09:51:41

问题


If a Customer has many Orders attached to them. How would you lazy load the Orders List using NHibernate.

Is it something that needs to be set up mapping file? any help or an example would be great.


回答1:


Heres a good article:

http://blogs.chayachronicles.com/sonofnun/archive/2007/03/30/230.aspx

From the above article:

The most common is to simply mark the class with the 'lazy="true"' attribute or place 'default-lazy="true"' in the mapping declaration:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="@core.assembly@"
 default-access="nosetter.camelcase-underscore" default-lazy="true">

Or

<class name="Cei.eMerge.Core.Domain.Contacts.Contact" table="Contact" lazy="true" >



回答2:


Chris' suggestion is how I'd do it, however if you want to do it at runtime you can set the Fetchmode on your criteria to be lazy like so:

criteria.SetFetchMode("Orders", FetchMode.Lazy)



回答3:


To lazy-load a particular collection of an entity, use "lazy=true" on the collection mapping. For instance:

<bag name="EmploymentHistory" cascade="all" inverse="true" lazy="true">
  <key column="PersonID" />
  <one-to-many class="MyDomain.EmploymentRecord, MyDomainAssembly" />
</bag>



回答4:


All of the answers here are correct, but if there are so many Orders you migth also want to use filters, so that you don't have to load all of them.

Customer customer = session.CreateCriteria(...)
              .SetFetchMode("Orders", FetchMode.Lazy)
              .UniqueResult<Customer>();

Ilist<Order> orders = session.CreateFilter(customer.Orders," WHERE this.OrderDate < ?")
                      .SetDateTime(...).List();



回答5:


Do you want that the Customer entity has a property that contains all his Orders ? I think that this can be quite a large collection.
I think that, the question you'll have to ask yourself is:
How many times do I really need to have direct access to the Orders of a Customer ?

Perhaps, in this case, you don't want to have a bidirectional association ? Perhaps you don't want to have an Orders collection in your Customer class.
Then, I would add a method to my Order Repository, which has the following signature:

IList<Order> GetOrdersForCustomer( Customer c );

But, I don't know if this is feasible for your situation.



来源:https://stackoverflow.com/questions/652305/lazy-loading-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!