Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

how to keep many to many relationships in sync with nhibernate?

I am creating data model for two objects Employee and Department. Employee has a list of departments and Department has a list of employees.

    class Employee{
             private IList<Department> _departments;
       public Employee()
       {
          _departments = new List<Department>();
       }
       public virtual ReadOnlyCollection<Department> Departments{
          get {return new ReadOnlyCollection<Department>(_departments);}
       }
     }

    class Department{
             private IList<Employee> _employees;
       public Department()
       {
          _departments = new List<Employee>();
       }
       public virtual ReadOnlyCollection<Employee> Employees{
          get {return new ReadOnlyCollection<Employee>(_employees);}
       }
     }

How do I write AddEmployee method in Department class and AddDepartment method in Employee class to make it in sync with nHibernate? I wrote this in Employee class

     public virtual void AddDepartment(Department department)
     {
        if (!department.Employees.Contains(this))
        {
            department.Employees.Add(this);
        }
        _departments.Add(department);

     }

But it doesnt work as I expected it to work.Can someone help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is an example of how I handle these relationships:

public class User
{
    private IList<Group> groups;
    public virtual IEnumerable<Group> Groups { get { return groups.Select(x => x); } }

    public virtual void AddGroup(Group group)
    {
        if (this.groups.Contains(group))
            return;

        this.groups.Add(group);
        group.AddUser(this);
    }

    public virtual void RemoveGroup(Group group)
    {
        if (!this.groups.Contains(group))
            return;

        this.groups.Remove(group);
        group.RemoveUser(this);
    }
}

My User mapping looks like this:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        //Id, Table etc have been omitted

        HasManyToMany(x => x.Groups)
            .Table("USER_GROUP_COMPOSITE")
            .ParentKeyColumn("USER_ID")
            .ChildKeyColumn("GROUP_ID")
            .Access.CamelCaseField()
            .Cascade.SaveUpdate()
            .Inverse()
            .FetchType.Join();
    }
 }

My Group mapping looks like this:

public class GroupMap : ClassMap<Group>
{
    public GroupMap()
    {
        //Id, Table etc have been omitted

        HasManyToMany(x => x.Users)
            .Table("USER_GROUP_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("USER_ID")
            .Access.CamelCaseField();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...