如何在nhibernate条件api中加入ISet和子对象

时间:2012-05-11 15:47:04

标签: nhibernate

如果我有以下课程

public class Order
{

    public virtual ISet<OrderItem> OrderItems { get; set; }

}


public class OrderItem
{

    public virtual Product Product { get; set; }

}

OrderItem与产品有一对一的关系。从一个sql请求获得所有这些是最有效的。

我正在使用nhibernate标准api。我如何将orderitems和产品添加为jointypes

        using (ITransaction transaction = _session.BeginTransaction())
        {
            try
            {
                Model.Order order = _session
                .CreateCriteria(typeof(Order))
                .Add(Restrictions.Eq("Id", id))
                .CreateAlias("OrderItems", "orderItems", JoinType.xxxx)

                .UniqueResult<Model.Order>();
                transaction.Commit();
                return order;
            }
            catch (HibernateException)
            {
                transaction.Rollback();
                _session.Close();
                _session.Dispose();
                throw;
            }
        }

1 个答案:

答案 0 :(得分:1)

Model.Order order = _session.CreateCriteria(typeof(Order))
    .Add(Restrictions.Eq("Id", id))
    .SetFetchMode("OrderItems", FetchMode.Eager)
    .UniqueResult<Model.Order>();