Nhibernate组件映射:从数据库查询时,Value Object中的父对象为null

时间:2010-10-31 18:06:34

标签: nhibernate fluent-nhibernate nhibernate-mapping value-objects

我将我的值Object Item作为组件映射到下面的映射配置

{
            Table("Product");
            Not.LazyLoad();
            Id(x => x.Id, "id");
            Map(x => x.Number, "number");
            Map(x => x.Name, "name");
            Map(x => x.Description, "description");
            Map(x => x.Status, "status");
            HasMany(x => x.ItemLines).Component(
                m =>
                {                                                                               
                        m.Map(x => x.ItemId, "itemid");
                        m.Map(x => x.Qty, "quantity");                      
                    }).Table("productitems").KeyColumn("itemid");
        }

Class structure 


public class ItemLine 
{
    public Product Product { get; set; }
    public Guid ItemId { get; set; }
    public int Qty { get; set; }




    public ItemLine()
    {

    }
    public ItemLine(Product product, Guid itemId, int qty)
    {
        Product = product;
        ItemId = itemId;
        Qty = qty;

    }

//Equality and GetHashCode implemented.....


}

我能够将数据插入数据库,但在按产品ID检索时,项目行中的Product属性为空。

我是否需要传递Mapping>

中的任何引用

请帮忙

谢谢,

1 个答案:

答案 0 :(得分:5)

确定。通过反复试验解决。

添加m.ParentReference(x => x.Product);

{ 
            Table("Product"); 
            Not.LazyLoad(); 
            Id(x => x.Id, "id"); 
            Map(x => x.Number, "number"); 
            Map(x => x.Name, "name"); 
            Map(x => x.Description, "description"); 
            Map(x => x.Status, "status"); 
            HasMany(x => x.ItemLines).Component( 
                m => 
                {                                                                                
                        m.Map(x => x.ItemId, "itemid"); 
                        m.Map(x => x.Qty, "quantity");

                        m.ParentReference(x => x.Product);   


                    }).Table("productitems").KeyColumn("itemid"); 
        } 

希望这有助于某人。