Running NHibernate Queries Without Joining

I have Fluent mappings that map the fictional Customer class to the list of orders. Now I want to get all Clients from the database without loading Orders. Can this somehow indicate in the request / criteria / etc or LazyLoading the only solution?

Fictional classes:

public class Customer
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Order> Orders { get; set; }
}

public class Order
{
    public virtual int Id { get; set; }
    // ++ 
}
+3
source share
2 answers

Lazy loading does this automatically anyway, are you trying to avoid lazy loading? Why?

For instance:

IList<Customer> customers = _session.CreateCriteria<Customer>().List<Customer>();

Will provide you all customers. Orders will only be called from the database when you call the collection of orders for the Customer object, for example:

foreach(Customer customer in customers)
{
    IList<Order> orders = customer.Orders; // Will hit the database
}

, 10 , 11 (1, , 10 ). SELECT N + 1, , , "", ​​ . , , ?


: . , POCO -, POCO Session NHibernate, ( NULL). , "", - , NHibernate , :

public IList<CustomerView> GetAllCustomers()
{
    return (from c in _session.Query<Customer>()
            select new CustomerView()
            {
                Id = c.Id,
                Name = c.Name
            }).ToList();
}

public CustomerDetail GetCustomerFromId(int id)
{
    return (from c in _session.Query<Customer>()
            where c.Id == id
            select new CustomerDetail()
            {
                Id = c.Id,
                Name = c.Name
                FullAddress = c.FormatFullAddress(),
                Orders = c.Orders,
                // other properties (snip)
            }).SingleOrDefault();
}

LINQ, NHibernate 3.0, NHibernate 3.0 Projections. , http://nhibernate.info/doc/nh/en/index.html#querycriteria-projection

+2

FetchMode :

var crit = session.CreateCriteria (typeof(SomeObject));
crit.SetFetchMode ("association", FetchMode.Eager);

, / . , .

+1

Source: https://habr.com/ru/post/1766159/


All Articles