简体   繁体   English

NHibernate不加载子对象

[英]NHibernate not loading child objects

I have following class and associated mappings (Fluent NHibernate): 我有以下类和相关的映射(Fluent NHibernate):

public class Category
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual Category ParentCategory { get; set; }
    public virtual IList<Category> ChildCategories { get; set; }
}

Mappings: 映射:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Table("Categories");

        Id(x => x.Id).Column("Id").CustomType("Int32").Access.Property()
         .CustomSqlType("int").Not.Nullable().Precision(10)
         .GeneratedBy.Identity();

        Map(x => x.Description).Column("Description").Nullable()
         .Generated.Never().CustomType(typeof (string)).Access
         .Property().Length(250);

        Map(x => x.Name).Not.Nullable().Generated.Never().CustomType("string")
         .Access.Property().Column("Name").Length(50);

        References(x => x.ParentCategory).Column("ParentCategoryId");
        HasMany(x => x.ChildCategories).KeyColumn("ParentCategoryId").Inverse()
         .AsBag().Fetch.Select();
    }
}

I creating two Category object as follows: 我创建两个Category对象,如下所示:

var c = new Category
        {
            Name = "Ebooks",
            Description = "Contains awz, mobi, pdf, epub and other 
                electronic books"
        };
var cc = new Category
         {
            Name = "Kindle Books",
            Description = "Contains Kindle ebook reader format books 
                    (awz, mobi)",
            ParentCategory = c
         };
session.SaveOrUpdate(c);
session.SaveOrUpdate(cc);

When I try to access saved objects: 当我尝试访问保存的对象时:

var c = session.Load<Category>(1);
var cc = c.ChildCategories;

c contains the Category object with Id of 1 but its ChildCategories property is null . c包含Id为1的Category对象,但其ChildCategories属性为null

What I am doing wrong? 我做错了什么?

PS:- This is my first experiment with NHibernate and so with Fluent NHibernate . PS:-这是我第一次使用NHibernate实验,因此也是使用Fluent NHibernate实验。

EDIT:- The following stuff worked. 编辑:-以下工作。 I needed to close the session before opening it again for reading. 我需要先关闭会话,然后才能再次打开它以进行阅读。 Otherwise it read just from memory nad as @Holf has pointed out I needed to add Child Category to Category like: 否则它只是从nad的内存中读取,因为@Holf指出我需要将Child Category添加到Category例如:

c.ChilCategories.Add(cc); c.ChilCategories.Add(cc);

I just did as follows: 我只是这样做如下:

var session = sf.OpenSession();
CreateCategory(session);//ADDED TWO CATEGORIES EBooks, Kindle Ebooks
session.Close();

session = sf.OpenSession();
FetchCategories(session);//READ CATEGORY EBooks AND ASSOCIATED CHILDREN
session.Close();

Although you've handled one side of the relationship, by assigning 'c' as the ParentCategory of 'cc', I can't see anywhere that you've done the inverse. 尽管您已经处理了关系的一方,但通过将'c'分配为'cc'的ParentCategory,我看不到您做了相反的任何事情。 I think you'll also need to do 我想你也需要做

c.ChildCategories.Add(cc); c.ChildCategories.Add(cc);

before doing the SaveOrUpdate. 在执行SaveOrUpdate之前。

Can you update your map to the following: 您可以将地图更新为以下内容吗:

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Table("Categories");

        Id(x => x.Id).Column("Id").CustomType("Int32").Access.Property()
         .CustomSqlType("int").Not.Nullable().Precision(10)
         .GeneratedBy.Identity();

        Map(x => x.Description).Column("Description").Nullable()
         .Generated.Never().CustomType(typeof (string)).Access
         .Property().Length(250);

        Map(x => x.Name).Not.Nullable().Generated.Never().CustomType("string")
         .Access.Property().Column("Name").Length(50);

        References(x => x.ParentCategory).Column("ParentCategoryId");
        HasMany(x => x.ChildCategories).KeyColumn("ParentCategoryId").Inverse()
         .Cascade.All();
    }
}

Also you cannot be sure that the Id with value 1 refers to the Parent Category, use the LInq provider to load the correct object. 同样,您不能确定值为1Id引用父类别,请使用LInq提供程序加载正确的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM