Nhibernate select N+1 (to avoid by using lazy loading which is by default)

Refer to http://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem

Refer to http://www.realsolve.co.uk/site/tech/hib-tip-pitfall.php?name=why-lazy

Problem:

Suppose we have a class Leaf with a many-to-one relationship with Tree. In other words, one Tree can be related to many Leafs. Lets suppose we want to list the name of the Leaf.(Refer to Many2One example) .

Here, set the lazy = false.

 <class name="Tree" table="Tree" lazy="false">

IList<Leaf> list = session.Query<Leaf>().ToList<Leaf>();

you could get all the leaves with 1 select (SELECT * from Leaf;), instead of N+1. With a large N, the performance hit can be very significant.

 

你可能感兴趣的:(Hibernate)