Eager Loading Using Fluent NHibernate/Nhibernate & Automapping

好久不见. 提交于 2019-11-28 19:17:15
Tim Hoolihan

each mapping has to have lazy loading off

in Node Map:

Map(x => x.EntityType).Not.LazyLoad();

in EnityType Map:

Map(x => x.Properties).Not.LazyLoad();

and so on...

Also, see NHibernate Eager loading multi-level child objects for one time eager loading

Added:

Additional info on Sql N+1:

http://nhprof.com/Learn/Alerts/SelectNPlusOne

I figure it out myself. The key is to use SetResultTransformer() passing an object of DistinctRootEntityResultTransformer as a parameter. So the query now looks like as follows

Session.CreateCriteria(typeof (Node))
   .SetFetchMode( "Etype", FetchMode.Join )
   .SetFetchMode( "Etype.Properties", FetchMode.Join )
   .SetFetchMode( "Etype.Properties.ListValues", FetchMode.Join )
   .SetResultTransformer(new DistinctRootEntityResultTransformer());

I found the answer to my questions through these links:

http://www.mailinglistarchive.com/html/nhusers@googlegroups.com/2010-05/msg00512.html

http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-associations-efficiently-with-nhibernate.aspx

I ended up with something like this:

HasMany(x => x.YourList).KeyColumn("ColumnName").Inverse().Not.LazyLoad().Fetch.Join()

Just make sure to select your entity like this, to avoid duplication due to the join:

session.CreateCriteria(typeof(T)).SetResultTransformer(Transformers.DistinctRootEntity).List<T>();

SetResultTransformer with DistinctRootEntityResultTransformer will only work for Main object but IList collections will be multiplied.

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