1

Deparment を表すオブジェクトがあります。部門には、多数の従業員と 1 つのサブ部門を含めることができます。Employee には、部下用の複数の Employee を含めることができます。Fluent NHibernate とのこの関係をどのように表すことができますか。ドメイン クラスは次のようになります。

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
}

そして、私のデータベーステーブルは次のようになります:

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.

データを選択してテーブルに挿入するための流れるような nhibernate マッピングを作成する方法。

4

1 に答える 1

0

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.

于 2013-01-10T14:24:21.320 に答える