如何在NHibernate中不重复加载关联?

如何在NHibernate中不重复加载关联?

我需要加载一个非常大的对象的列表,里面有这么多的孩子和孩子。最好的办法是什么?

我使用的是Oracle 11g数据库,我编写了以下方法,但结果是笛卡儿产品(复制的结果):

 public IList<ARNomination> GetByEventId(long eventId)
        {
            var session = this._sessionFactory.Session;

            var nominationQuery = session.Query<ARNomination>().Where(n => n.Event.Id == eventId);

            using (var trans = session.Transaction)
            {
                trans.Begin();

                // this will load the Contacts in one statement
                nominationQuery                    .FetchMany(n => n.Contacts)
                    .ToFuture();

                // this will load the CustomAttributes in one statement
                nominationQuery                    .FetchMany(n => n.CustomAttributes)
                    .ToFuture();

                // this will load the nominations but joins those two tables in one statement which results in cartesian product
                nominationQuery                    .FetchMany(n => n.CustomAttributes)
                    .FetchMany(n => n.Contacts)
                    .ToFuture();

                trans.Commit();
            }

            return nominationQuery.ToList();
        }


蛊毒传说
浏览 500回答 2
2回答

开心每一天1111

只要你急于加载多个集合,那么笛卡尔产品就会出现。为了选择合适的批次大小,我可能会选择与page size因为感觉很好.。(但没有证据)然而,无论你走哪条路,我建议你向结果扔一个分析器,看看哪个对你有更好的效果.
打开App,查看更多内容
随时随地看视频慕课网APP