0

I have a hierarchy category table like this

Id int,
Description varchar(100),
ParentId int,
Ordinal int,
IsActive bit

I want to fetch all of the Categories from parent to child, so when I called session.get<Category>(id), it already fetched all of their children. Here is my map and class:

class Category
{
    public virtual int Id {get; set;}
    public virtual string Description {get; set;}
    public virtual int ParentId {get; set;}
    public virtual int Ordinal {get; set;}
    public virtual bool IsActive {get; set;}
}

class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
         Table("TB_CATEGORY");
         Id(f => f.Id).GeneratedBy.Native();
         Map(f => f.Description);
         Map(f => f.ParentId);
         Map(f => f.Ordinal);
         Map(f => f.IsActive);
    }
}

I've searched so many articles, and am still confused when using their solutions because they don't tell me about the table structure and the mappings. Like this one from ayende blog, I think its a good solution, but I can't follow it well enough to apply this in my project. Could somebody give me a step by step tutorial to achieve this? Are my mapping and class correct?

bcr
  • 1,983
  • 27
  • 30
Dion Dirza
  • 2,575
  • 2
  • 17
  • 21

1 Answers1

1

using the following classes

class Category
{
    public virtual int Id {get; private set;}
    public virtual string Description {get; set;}
    public virtual Category Parent {get; set;}
    public virtual bool IsActive {get; set;}
    public virtual IList<Category> Children {get; private set;}

    public override bool Euqals(object obj)
    {
        var other = obj as Category;
        return other != null && (Id == 0) ? ReferenceEquals(other, this) : other.Id == Id;
    }

    public override int GetHashCode()
    {
        return Id;
    }
}

class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
         Table("TB_CATEGORY");
         Id(f => f.Id).GeneratedBy.Native();
         Map(f => f.Description);
         References(f => f.Parent).Column("ParentId");
         HasMany(f => f.Children)
             .AsList(i => i.Column("Ordinal"))      // let the list have the correct order of child items
             .KeyColumn("ParentId")
             .Inverse();              // let the reference maintain the association
         Map(f => f.IsActive);
    }
}

then you can query

var categoriesWithChildrenInitialised = session.QueryOver<Category>()
    .Fetch(c => c.Children).Eager
    .List()
Firo
  • 30,626
  • 4
  • 55
  • 94
  • thanks for the mapping and class code u have provide, it's really help me a lot to understand better. But as i try the query code above, the result is wrong and so many record was return and AsList method didnt take string as argument anymore. I using NH v3.3 maybe u had using different version than mine – Dion Dirza Jun 08 '13 at 09:01
  • AsList now takes a lambda for configuring the list mapping, i fixed the code. I guess the results are too many because Equals and GetHashCode is flawed. I added a simple implementation to the code. – Firo Jun 09 '13 at 10:39