Lazy Loading in NHibernate

前端 未结 5 1075
不知归路
不知归路 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条回答
  •  暗喜
    暗喜 (楼主)
    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();
    
    Ilist orders = session.CreateFilter(customer.Orders," WHERE this.OrderDate < ?")
                          .SetDateTime(...).List();
    

提交回复
热议问题