看看我的例子:
Student 继承User User.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="StudentMIS.Common.Data.User, StudentMIS.Common" table="[User]">
<id name="ID" column="UserID" type="Int32">
<generator class="assigned" />
</id>
....
<joined-subclass name="StudentMIS.Common.Data.Student, StudentMIS.Common" lazy="false" table="Student">
<key column="StudentID"/>
.....
<!--映射学生对象到成绩对象-->
<bag name="Scores" cascade="all" inverse="true" lazy="false" >
<key column="StudentID" />
<one-to-many class="StudentMIS.Common.Data.Score, StudentMIS.Common" />
</bag>
</class>
</hibernate-mapping>
Student 与 Score 是一对多(ont-to-many)的的关系:score.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="StudentMIS.Common.Data.Score, StudentMIS.Common" table="Score">
<id name="ID" column="ScoreID" type="Int32">
<generator class="identity" />
</id>
....
<many-to-one name="Student" column="StudentID" not-null="false" />
</class>
</hibernate-mapping>
假设我们已经有StudentID=20020724的持久实例存在,现在我们为其添加一成绩实例:
Score score = new Score();
score.Subject = "C++ Promgraming";
score.Credit = 3;
...
ITransaction trans = session.BeginTransaction();
Student student = (Student)session.Load(typeof(Student),20020724);
student.Score.Add(score);
score.Student = student;
session.Save(score); //在这里,这一步是必须,因为我们的Student是从User继承过来的,
trans.Commit();