nHibernate中同时实现继承与多对一/一对多映射

看看我的例子:

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值