I have the following model class for my Category object :
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual IList<Category> SubCategories { get; set; }
This is my free nhibernate mapping class for ' Category ':
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
References(x => x.ParentCategory).Column("ParentCategoryId");
HasMany(x => x.SubCategories).Where(x => x.Id == x.ParentCategory.Id);
My database, to which this refers, consists of several "categories", some of which are at the root level (and have the value ParentCategoryId = NULL), and all the rest are subcategories, which can only be at 1 level of depth or can be 3 , 4,5 levels (recursive parents return to the root / parent CategoryId.
An example of the relationship between rows / records:
Cars (Id = 1 - ParentCategoryId = NULL)
Cars (Id = 1) > Hatchback (Id = 2 - ParentCategoryId = 1)
Cars (Id = 1) > Hatchback (Id = 2) > Ford (Id = 3 - ParentCategoryId = 2)
Motorcycles (Id = 4 - ParentCategoryId = NULL)
Motorcycles (Id = 4) > Scooters (Id = 5 - ParentCategoryId = 4)
SubCategories ' ParentCategoryId (Id), . HasMany, , .