Eager Loading Using Fluent NHibernate/Nhibernate & Automapping

前端 未结 4 513
萌比男神i
萌比男神i 2020-12-13 19:34

I have a requirement to load a complex object called Node...well its not that complex...it looks like follows:-

A Node has a refere

相关标签:
4条回答
  • 2020-12-13 19:47

    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

    0 讨论(0)
  • 2020-12-13 19:52

    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

    0 讨论(0)
  • 2020-12-13 19:54

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

    0 讨论(0)
  • 2020-12-13 20:03

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