zoukankan      html  css  js  c++  java
  • Nhibernate教程2(3)

    2)       含有关系的表的情况

    含有关系的表指的是像学生这样,除了保存学生的基本信息,还希望把选课信息保存到学生的类中。这样情况下不能用软件来辅助产生对应的类和XML,这是NHibernate中唯一需要费脑筋学的地方。学生表对应的类和XML如下所示:


    类文件(Student.cs

    using System;

    using Iesi.Collections;

    namespace CodeTest{

             public class Student{
                       public Student(){}

                       private int studentId;

                       private string studentName;

                       private string studentPassword;

                       private string emailAddress;

                       private DateTime dateCreated;

                       private ISet courses =

    new HashedSet();

                       public int StudentId{

                                get { return studentId; }

                                set { studentId = value; }

                       }

                       public string StudentName{

                                get { return studentName; }

                                set { studentName = value; }         }

                       public string StudentPassword{

                                get { return studentPassword; }

                                set { studentPassword = value; }                   }

                       public string EmailAddress{

                                get { return emailAddress; }

                                set { emailAddress = value; }}

    public DateTime DateCreated{

                                get { return dateCreated; }

                                set { dateCreated = value; }}

                       public ISet Courses{

                                get{return courses;}

                                set{courses=value;}

                       }                

             }

    }

    XML文件(Student.hbm.xml

    <?xml version="1.0" encoding="utf-8" ?>

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

             <class name="NHibernateTest.Student, NHibernateTest" table="Student">

                       <id name="StudentId" column="StudentId" type="Int32">

                                <generator class="native" />

                       </id>

                       <property name="StudentName" column= "StudentName" type="string" length="40"/>

                       <property name="StudentPassword" type="string" length="20"/>

                       <property name="EmailAddress" type="String" length="40"/>

                       <property name="DateCreated" type="DateTime" length="8"/>

                      <set name="Courses" table="StudentCourse" inverse="false">

                       <key column="StudentID"/>

                       <many-to-many column="CourseID" class=" NHibernateTest.Course, NHibernateTest "/>

                       </set>

    </class>

    </hibernate-mapping>

  • 相关阅读:
    [置顶] 图书推荐:SQL Server 2012 T-SQL基础 Itzik Ben-Gan
    UVA1366-----Martian Mining------DP
    动态规划——最长公共子序列(LCS)
    需求分析挑战之旅(疯狂的订餐系统)(3)——背景-需要-需求规格
    JavaScript学习笔记(四十四) 装饰器
    C中的volatile用法
    Java注解Annotation学习
    非常好!讲逻辑回归的,讲得很透彻
    RPC的学习 & gprotobuf 和 thrift的比较
    僵尸进程学习 & 进程状态列表 & Linux信号学习
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1538339.html
Copyright © 2011-2022 走看看