NHibernate的多对多的关系映射 [英] Nhibernate many to many relationship mapping

查看:111
本文介绍了NHibernate的多对多的关系映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新NHibernate的。我想使用NHibernate作为映射数据库工具来开发asp.net应用程序。

I am very new to Nhibernate. I would like to develop an application in asp.net by using NHibernate as mapping database tools.

我有以下表模式:

CREATE TABLE [dbo].[tblTeam](
    [TeamID] [int] IDENTITY(1,1) NOT NULL,
    [TeamName] [varchar](100) NOT NULL
)


CREATE TABLE [dbo].[tblEmployee](
    [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
    [EmployeeName] [varchar](100) NOT NULL
)

CREATE TABLE [dbo].[tblTeamEmployee](
    [TeamID] [int] NOT NULL,
    [EmployeeID] [int] NOT NULL
)

这是我做了NHibernate的映射文件:

And here is Nhibernate mapping file that I have made:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Team, TelDir.Core" table="tblTeam" lazy="false">
    <id name="ID" column="TeamID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="TeamName" column="TeamName" />

    <bag name="Employees" cascade="none" table="tblTeamEmployee" lazy="false" access="readonly">
      <key column="EmployeeID"/>
      <many-to-many class="TelDir.Core.Domain.Employee, TelDir.Core" column="TeamID"/>
    </bag>

  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="TelDir.Core.Domain.Employee, TelDir.Core" table="tblEmployee" lazy="false">
    <id name="ID" column="EmployeeID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="EmployeeName" column="EmployeeName" />        

    <bag name="Teams" cascade="none" table="tblTeamEmployee" lazy="false" >
      <key column="EmployeeID"/>
      <many-to-many class="TelDir.Core.Domain.Team, TelDir.Core" column="EmployeeID"/>
    </bag>

  </class>
</hibernate-mapping>

和我的POCO类定义如下:

And my POCO class is defined as below:

namespace TelDir.Core.Domain
{
    public class Team   {

        private string _TeamName = "";
        private IList<Employee> _employees = new List<Employee>();

        public Team() { }

        public  string TeamName {
            get { return _TeamName }
            set { _TeamName = value; }
        }


        public  IList<Employee> Employees
        {
            get { return new List<Employee>(_employees).AsReadOnly(); }
            protected set { _employees = value; }          
        }

        public  void AddEmployee(Employee em)
        {
            if (!_employees.Contains(em)){
                _employees.Add(em);
            }
        }

        public  void RemoveEmployee(Employee em)
        {
            if (_employees.Contains(em)){
                _employees.Remove(em);
            }
        }  
    }
}




namespace TelDir.Core.Domain
{
    public class Employee  {

        private string _EmployeeName = "";
        private IList<Team> _teams = new List<Team>();

        public Employee() { }

        public  string EmployeeName {
            get { return _EmployeeName}
            set { _EmployeeName = value; }
        }


        public  IList<Team> Teams
        {
            get { return new List<Team>(_teams).AsReadOnly(); }
            protected set { _teams = value; }          
        }

        public  void AddTeam(Team tm)
        {
            if (!_teams.Contains(tm)){
                _teams.Add(tm);
            }
        }

        public  void RemoveTeam(Team tm)
        {
            if (_teams.Contains(tm)){
                _teams.Remove(tm);
            }
        }  
    }
}

我不知道我的映射和实体类是否明确?

I am not sure whether my mapping and entity class is well defined?

假如我有以下我的表中的数据

Suppose I have following data in my table

tblTeam
        TeamID     |      TeamName
        --------------------------
          1        |         A
        --------------------------
          2        |         B

tblEmployee
        EmployeeID |      EmployeeName
        ------------------------------
          1        |      Jhon
        ------------------------------
          2        |      Michel
        ------------------------------
          3        |      Lino

tblTeamEmployee
        TeamID     |      EmployeeID
        ------------------------------
          1        |      1
        ------------------------------
          1        |      2
        ------------------------------
          2        |      3
        ------------------------------
          2        |      1        

我如何删除员工姓名='约翰'在ASP.NET页面团队='B'?

How can I remove Employee name='Jhon' from Team='B' in ASP.NET page?

推荐答案

当你想改变你的收藏,删除 .AsReadOnly()从它。

As you want to change your collection, remove the .AsReadOnly() from it.

然后,如果你同时拥有的ID(1和2),只是做:

Then, if you have the ids of both (1 and 2), just do:

var teamB = session.Get<Team>(2);
teamB.Employees.Remove(teamB);
session.Flush();

这篇关于NHibernate的多对多的关系映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆