2012-05-11 78 views
0

,如果我有以下類如何加入的ISet和子對象在NHibernate的標準API

public class Order 
{ 

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

} 


public class OrderItem 
{ 

    public virtual Product Product { get; set; } 

} 

OrderItem的與產品一一對應的關係。從一個sql請求中獲取所有這些信息最有效。

我使用NHibernate的標準API。我將如何添加orderitems和產品作爲聯合類型

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