Lazy Loading in NHibernate

前端 未结 5 1041
不知归路
不知归路 2021-01-19 10:47

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

相关标签:
5条回答
  • 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>
    
    0 讨论(0)
  • 2021-01-19 10:55

    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)
    
    0 讨论(0)
  • 2021-01-19 10:57

    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" >
    
    0 讨论(0)
  • 2021-01-19 11:13

    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.

    0 讨论(0)
  • 2021-01-19 11:15

    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();
    
    0 讨论(0)
提交回复
热议问题