nhibernate 中带有连接子类的多重继承
我有两个基类 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; }
}
课程代码.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 在两个基类(学生、课程)中加入子类时,会出现如下错误:
< strong>{“重复类/实体映射Core.Model.StudentCourse”}
我知道通过连接子类创建相同的子类是不可能的...... 请有人告诉我在这种情况下我应该做什么才能使多重继承成为可能......
提前致谢......
I have two Base classes Student and Course and one subclass StudentCourse which inherit properties of these two base classes.
Is it possible to do such inheritance with Joined subclass?
I have tried by creating interfaces for both Student and Course class as below:
public interface StudentInterface
{
Int32 StudentId { get; set; }
}
public interface InterfaceCourse
{
Int32 CourseId { get; set; }
}
Code for student.cs
it implements 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; }
}
code for course.cs
public class Course:InterfaceStudentCourse
{
[Key]
public virtual Int32 **CourseId** { get; set; }
public virtual string Name { get; set; }
}
Code for studentCourse.cs
it implements both base classes Student and Course
Here i want to make StudentCourse own primary key,not of it should be of superclass primary key.They should be foreign here as shown in StudentCourse.cs below
public class StudentCourse:StudentInterface,InterfaceCourse
{
[Key]
public virtual Int32 **StudentCourseId** { get; set; }
public override Int32 StudentId { get; set; }
public virtual Int32 CourseId { get; set; }
}
Mapping files are:
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>
So the problem is when i made the StudentCourse joined subclass in only one base class,then it gives error like :
{"Could not find a getter for property 'StudentName' in class 'Core.Model.StudentCourse'"}
and when i made StudentCourse joined subclass in both base classes (student,course),then error like:
{"Duplicate class/entity mapping Core.Model.StudentCourse"}
I know creating same subclass by joined subclass is not possible....
Will anyone plz tell me wat should i do to make multiple inheritance possible in such scenario.....
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先我要澄清的是,在 NH 中不可能通过连接子类或子类来进行多重继承。
另一种认为在加入子类时,您所采取的示例是错误的(我认为是这样),
在加入子类中,您必须有一个像动物这样的父类,并且您可以通过使用加入子类将其继承给猫,狗等。 ..
关于您的示例,最好使用法线映射,例如
--将Student_Course中的studID和CourseID作为复合或对Student_Course、StudID和CustomerID进行PK,
但根据我的经验,最佳实践是制作复合材料具有一对多的关系..
希望这会对您有所帮助。
at the first i will clear you that it is not possible to do Multiple inheritance by joined-subclass or subclass in NH.
another think at the point of joined-subclass the example you are taking is wrong (i think so)
in joined subclass u must have an parent class like animal and you can inherit this to cat,dog etc..by using joined-subclass...
about your example it is best to use normal mapping like--
take studID and CourseID in Student_Course as composite or take an PK for Student_Course and StudID and CustomerID
but as per my exp it is best practice to make composite with one-to-many relationship..
hope this will help you.