1
votes

I have an object that represents a Deparment. Department can contain many Employee and one SubDepartment. Employee can contain many Employee for Subordinates. How can I represent this relationship with Fluent NHibernate. The Domain class looks like this:

public class Department : Entitybase
{
    public int Id;
    public string DepartmentName;
    public List<Employee> Employees;
    public Department SubDepartment;

}

public class  Employee : EntityBase
{
    public int Id;
    public string Name;
    public List<Employee> Subordinates
}

And My Database Tables look likes :

Department Table
   Id: int
   SubDepartmentId : int // a sub department id
   DepartmentName : string


Employee Table
    Id : int
    SuperviserId : int // A Superviser Id
    Name : string
    DepartmentId : int // a department id that contain this employee.

How to create fluent nhibernate mapping for Select and Insert a Data to the table.

1
It would be good look at the mappings that you have come up with so far. Could you add those to the question? - Suhas

1 Answers

0
votes

Please take a look at Ayende's post on efficiently selecting a tree:

http://ayende.com/blog/4151/nhibernate-tips-tricks-efficiently-selecting-a-tree

Next are maps as I understand you want them

  • DepartmentMap:

For mapping the employees on the department you could write something like this.

Map(d => d.Id);
Map(d => d.DepartmentName);

HasMany(d => d.Employees)
.KeyColumn("DepartmentId")
.Cascade.None();

Then, for the subdepartment property, I don't exactly get how one department has only one subdepartment. Did you want this to be as a list of subdepartments?

References(d => d.Subdepartment)
.Column("SubDepartmentId")
.Cascade.All();

The last Cascade deletes the subdepartment when you delete this department

  • EmployeeMap

    Map(e => e.Id); Map(e => e.Name); HasMany(e => e.Subordinates) .Column("SuperviserId") .Cascade.None(); //in case this is nullable, please view other tyes of cascading and modify as you want.