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); }
感谢您的阅读,坚持每天进步一点点,离成功就更新一步;希望文章对您有所帮助;源代码下载