• NHibernate初学六之关联多对多关系


    1:创建三张表T_Course、T_Student、T_Middle;其中一个学生可以对应多个课程,一个课程也可以对应多个学生,用T_Middle存放它们的关系内容;

    复制代码
    CREATE TABLE [dbo].[T_Course](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [CourseName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
     CONSTRAINT [PK_T_Course] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]
    
    
    CREATE TABLE [dbo].[T_Student](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [Name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
        [StudentNum] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,
     CONSTRAINT [PK_T_Student] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]
    
    
    CREATE TABLE [dbo].[T_Middle](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [StudentID] [int] NULL,
        [CourseID] [int] NULL,
     CONSTRAINT [PK_T_Middle] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]
    复制代码

    2:实体的代码,其中两个类中都有IList,中间关系类就不用创建的:

    复制代码
        public class CourseModel
        {
            public virtual int Id { get; set; }
            public virtual string Coursename { get; set; }
            public virtual IList Students { get; set; }
        }
    
        public class StudentModel
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual string Studentnum { get; set; }
            public virtual IList Courses { get; set; }
        }
    复制代码

    3:XML映射文件的内容

    3.1 CourseModel.hbm.xml文件的内容:

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <hibernate-mapping assembly="Wujy.ModelLibrary" namespace="Wujy.ModelLibrary.Entity" xmlns="urn:nhibernate-mapping-2.2">
      <class name="CourseModel" table="T_Course" lazy="true" >
        <id name="Id" column="ID">
          <generator class="identity" />
        </id>
        <property name="Coursename">
          <column name="CourseName" sql-type="varchar" not-null="false" />
        </property>
        <bag name="Students" table="T_Middle" lazy="true" inverse="true">
          <key column="CourseID"></key>
          <many-to-many column="StudentID" class="StudentModel"></many-to-many>
        </bag>
      </class>
    </hibernate-mapping>
    复制代码

    3.2 StudentModel.hbm.xml文件的内容:

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <hibernate-mapping assembly="Wujy.ModelLibrary" namespace="Wujy.ModelLibrary.Entity" xmlns="urn:nhibernate-mapping-2.2">
      <class name="StudentModel" table="T_Student" lazy="true" >
        <id name="Id" column="ID">
          <generator class="identity" />
        </id>
        <property name="Name">
          <column name="Name" sql-type="varchar" not-null="false" />
        </property>
        <property name="Studentnum">
          <column name="StudentNum" sql-type="varchar" not-null="false" />
        </property>
        <bag name="Courses" table="T_Middle" lazy="true" inverse="false">
          <key column="StudentID"></key>
          <many-to-many column="CourseID" class="CourseModel"></many-to-many>
        </bag>
      </class>
    </hibernate-mapping>
    复制代码

    注意:inverse:意思是反转,它指明类之间的关系由谁来进行维护。例如:班级(Class类)与学生(Student),班级-学生就是one-to-many,学生-班级就是many-to-one。它只能在 one-to-many中many的一方进行设置(当然,在many一方,可能是在<set>也可能是在<bag>也可能是在<array>中进行设置),而对于one方,是不进行Inverse设置的。在NHibernate 社区,inverse默认值为false。


    4: 主要代码如下

    复制代码
            protected void Button1_Click(object sender, EventArgs e)
            {
                CourseModel coursemodel = new CourseModel();
                StudentModel studentModel = new StudentModel();
                coursemodel.Coursename = "软件工程";
                int CID = CourseBll.NewAdd(coursemodel);
                
                studentModel.Name = "踏浪帅";
                studentModel.Studentnum = "20098991";
                studentModel.Courses = new List<CourseModel>();
                studentModel.Courses.Add(coursemodel);
                StudentBll.NewAdd(studentModel);
                
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                CourseModel coursemodel = new CourseModel();
                coursemodel.Coursename = "软件工程";
                CourseBll.Add(coursemodel);
            }
    
            protected void Button3_Click(object sender, EventArgs e)
            {
                StudentModel model = StudentBll.findById(1);
                StudentBll.Remove(model);
            }
    复制代码

    感谢您的阅读,坚持每天进步一点点,离成功就更新一步;希望文章对您有所帮助;源代码下载

    转载至:http://www.cnblogs.com/wujy/p/3602300.html

  • 相关阅读:
    Idea中创建maven项目
    安装axios
    配置路由
    安装Mock.js
    安装SCSS
    Incorrect username or password ( access token )
    Navicat使用笔记06Navicat Premium 12的安装、激活
    dump.rdb file All In One
    如何把闲置的 Mac mini 搭建成一个局域网中的 Web 服务器 All In One
    VSCode walkthroughs All In One
  • 原文地址:https://www.cnblogs.com/armyfai/p/3610579.html
Copyright © 2020-2023  润新知