【问题标题】:Multiple Inheritance in nhibernate with joined subclass带有连接子类的nhibernate中的多重继承
【发布时间】:2012-02-17 06:47:45
【问题描述】:

我有两个基类 Student 和 Course 以及一个继承这两个基类属性的子类 StudentCourse。

是否可以使用 Joined 子类进行这种继承?

我尝试为 Student 和 Course 类创建接口,如下所示:

public interface StudentInterface
    {
        Int32 StudentId { get; set; }

    }

 public interface InterfaceCourse
    {

        Int32 CourseId { get; set; }

    }

student.cs 代码

它实现了 StudentInterface

public class Student:StudentInterface 
    {
        [Key]
        public virtual Int32 **StudentId** { get; set; }
        public virtual string StudentName { get; set; }
        public virtual  DateTime Dob { get; set; }
        public virtual string Gender { get; set; }
        public virtual string Email { get; set; }
}

course.cs 的代码

public class Course:InterfaceStudentCourse
    {
        [Key]
        public virtual Int32 **CourseId** { get; set; }
        public virtual string Name { get; set; }
    }

studentCourse.cs 代码 它实现了基类 Student 和 Course 在这里我想让StudentCourse自己的主键,而不是它应该是超类主键。它们应该是外来的,如下面的StudentCourse.cs所示

public class StudentCourse:StudentInterface,InterfaceCourse
    {
        [Key]
        public virtual Int32 **StudentCourseId** { get; set; }
        public override Int32  StudentId { get; set; }
        public virtual Int32 CourseId { get; set; }
   }

映射文件是:

StudentInterface.hbm.xml

<class name ="StudentInterface" table="StudentInterface" >
    <id name="StudentId" type="Int32" column="StudentId" >
      <generator class="native"/>
    </id>

    <joined-subclass name="Student">
      <key column="StudentId"/>
      <property name="StudentName"/>
      <property name="Dob"/>
      <property name="Gender"/>
      <property name="Email"/>
*// Need this subclass in both Student and course.....but giving duplication entity error*
      **<joined-subclass name="StudentCourse">
        <key column="StudentCourseId"/>
        <property name="StudentId"/>
        <property name="CourseId"/>
     </joined-subclass>**

    </joined-subclass>

  </class>

CourseInterface.hbm.xml

<class name ="InterfaceCourse" table="InterfaceCourse" >
    <id name="CourseId" type="Int32" column="CourseId" >
      <generator class="native"/>
    </id>


    <joined-subclass name ="Course" >
      <key column="CourseId"/>
      <property name="Name"/>
   *// Need this subclass in both Student and course.....but giving duplication entity error*
**<joined-subclass name="StudentCourse">
            <key column="StudentCourseId"/>
            <property name="StudentId"/>
            <property name="CourseId"/>
         </joined-subclass>**
    </joined-subclass>
  </class>

所以问题是当我让 StudentCourse 只在一个基类中加入子类时,它会给出如下错误:

{“在'Core.Model.StudentCourse'类中找不到属性'StudentName'的getter”}

当我让 StudentCourse 在两个基类(学生、课程)中加入子类时,出现如下错误:

{“重复的类/实体映射 Core.Model.StudentCourse”}

我知道通过加入的子类创建相同的子类是不可能的...... 有人会告诉我在这种情况下我应该怎么做才能使多重继承成为可能.....

提前致谢...

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

    首先我会明确告诉你,在 NH 中不能通过joined-subclass 或 subclass 进行多重继承。

    另一个想法是在加入子类的时候,你所举的例子是错误的(我认为是这样)

    在加入的子类中,你必须有一个像动物这样的父类,你可以通过使用加入的子类将它继承给 cat、dog 等...

    关于您的示例,最好使用法线映射,例如--

    将 Student_Course 中的 studID 和 CourseID 作为复合材料或对 Student_Course 和 StudID 和 CustomerID 进行 PK

    但根据我的经验,最好的做法是使用一对多关系进行复合..

    希望这会对你有所帮助。

    【讨论】:

    • 谢谢 Steav....你清除了我的疑虑....现在我已经将 StudentCourseId 作为 Pk 和 Studentid 和 courseId 作为 FK...它有效....
    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 2011-03-02
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    相关资源
    最近更新 更多