2012-02-08 71 views
0

我有一個帶有ReferencesAny地圖的類。對於某些查詢,我在任何地圖上都會出現N + 1的情況。有沒有辦法使用Fetch獲取任何地圖?NHibernate ReferencesAny Fetching?

這裏是我的LINQ查詢是什麼樣子(這工作,但性能差,由於N + 1):

return Session.Query<Path>().Where(p => {condition}).ToArray().Select(p => p.ContentsObject); 

當我添加一個取到組合:

return Session.Query<Path>().Where(p => {condition}).Fetch(p => p.ContentsObject).Select(p => p.ContentsObject); 

查詢打起來說有一個無效的加入。

例外:

無效加入:p.Page [.Fetch [wp.cms.DomainModel.Entity.Structure.PagePathMemento,wp.cms.DomainModel.Entity.Base.ILocatablePage](其中[可溼性粉劑。 cms.DomainModel.Entity.Structure.PagePathMemento](NHibernate.Linq.NhQueryable`1 [wp.cms.DomainModel.Entity.Structure.PagePathMemento],Quote((p,)=>(.Any [wp.cms.DomainModel。 Entity.Users.UserSite](p.Site.Users,(u,)=>(Guid.op_Equality(u.User.Id,p1)),))),),Quote((p,)=>(p .Page)),)] 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:NHibernate.Hql.Ast.ANTLR.InvalidPathException:無效連接:p.Page [.Fetch [wp.cms.DomainModel.Entity.Structure.PagePathMemento,wp.cms.DomainModel.Entity.Base.ILocatablePage ](。)其中[wp.cms.DomainModel.Entity.Structure.PagePathMemento](NHibernate.Linq.NhQueryable`1 [wp.cms.DomainModel.Entity.Structure.PagePathMemento],Quote((p,)=>(.Any [wp.cms.DomainModel.Entity.Users.UserSite](p.Site.Users,(u,)=>(Guid.op_Equality(u.User.Id,p1)),))),),Quote(( p,)=>(p.Page)),)]

+0

顯示完整的異常+內部異常的消息。 – gdoron 2012-02-08 20:37:14

回答

4

我不認爲有可能急於加載any映射。

爲了避免N + 1,設置在您的實體(例如20或50)中的合理batch-size

參見19.1.5 Using batch fetching

+0

由於某些原因,它不會讓我設置批量大小。當我嘗試Session.SetBatchSize(50)時,它給了我錯誤:「沒有爲會話工廠定義批處理大小,批處理禁用,請設置adonet.batch_size = 1以啓用批處理。」我使用NH GuidComb密鑰生成器,並在我的配置中設置了批量大小屬性。有任何想法嗎? – Jereme 2012-02-08 23:19:35

+0

這不是我指的批量大小。見[5.1.3 class(13)](http://nhforge.org/doc/nh/en/index.html#mapping-declaration-class) – 2012-02-09 02:17:05

+0

現在我明白了。非常感謝!這是一個很大的幫助。 – Jereme 2012-02-09 16:52:42