nhibernate:如何初始化子列表对象 [英] nhibernate : how to initialise child list objects

查看:83
本文介绍了nhibernate:如何初始化子列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的存储库中有以下方法.这工作正常,我的orderItems已按预期初始化,但是orderItems包含另一个名为OrderItemAddress的集合.这些未初始化.我该怎么办?

I have the following method in my repository. This works fine, my orderItems are initialised as intended however orderItems contains another collection called OrderItemAddress. These are not initialised. How would i do this?

public Model.Order Get(int id)
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        Model.Order order = session
            .CreateCriteria(typeof(Model.Order))
            .Add(Restrictions.Eq("Id", id))
            .UniqueResult<Model.Order>();

        NHibernateUtil.Initialize(order.OrderItems);
        return order;
    }
}

推荐答案

首先,您可能只想使用联接对数据库服务器发出一个查询,而不要在之后初始化集合.如此获取订单:

First of all you might want to issue only one query to the db server using a join, and not initialize the collection after the order is fetched, like this:

public Model.Order Get(int id)
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        Model.Order order = session
            .CreateCriteria(typeof(Model.Order))
            .Add(Restrictions.Eq("Id", id))
            .SetFetchMode("OrderItems", FetchMode.Join)
            .UniqueResult<Model.Order>();

        return order;
    }
}

另一件事是渴望加载集合的集合,而又不会大幅度地损害性能.在您的情况下做到这一点的一种方法是:

Another thing is to eager load a collection of collections without hurting performance wildly. One way to do that in you scenario is this:

var orderCriteria = DetachedCriteria.For<Order>()
    .SetFetchMode("OrderLines", FetchMode.Eager)
    .Add(Restrictions.Eq("Id", orderId));

var orderLinesCriteria = DetachedCriteria.For<OrderLine>()
    .CreateAlias("Order", "order")
    .SetFetchMode("Addresses", FetchMode.Eager)
    .Add(Restrictions.Eq("order.Id", orderId));

IList list = s.CreateMultiCriteria()
    .Add(orderCriteria)
    .Add(orderLinesCriteria)
    .List();

var order = ((IList)list[0]).Cast<Order>().First();

不幸的是,这还没有测试,我以后可以这样做. 想法是进行一次多查询,一次获得所有所需的实体(这可能不是最有效的数据库查询,但至少只有一次行程),然后让会话从这两个结果中拼接实际的图形套.

This is unfortunately not tested yet, I am able to do that later. The idea is to make a multiquery that gets all the needed entities in one go (it might not be the most efficient db query possible, but it's at least only one trip) and then let the session stitch up the actual graph from the two result sets.

在我当前的项目中,一个稍有不同的用例可以正常工作,但是我不确定这里显示的用例是否完全正确.但我会继续谈一谈:)

A slightly different use case is working fine in my current project, but I'm a little unsure if the I have shown here, is completely correct. But I will return on that one :)

上面的代码已更改为实际有效的东西,现在已经过测试. 很抱歉,实体和集合的重命名.我为测试项目将它们重命名了.

The above code is changed to something that actually works and is now tested. Sorry for the renaming of the entities and collections. I renamed them slightly for my test project.

这篇关于nhibernate:如何初始化子列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆