映射hbm文件

时间:2018-07-14 13:33:28

标签: c# .net nhibernate mapping

H想在我的应用程序中使用继承,但是当我运行时,我的hbm映射文件有错误。我的代码在这里

 public class StudentDao
    {

        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual StudentDegreeType Degree { get; set; }
        public virtual string Field { get; set; }
        public virtual IEnumerable<StudentCourse> StudentCourses { get; set; }

我的映射文件是:

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Tosan.Sevrice.DataAccess" namespace="Tosan.Sevrice.DataAccess.Dao" xmlns="urn:nhibernate-mapping-2.2">
  <class name="StudentDao" table="STUDENTT" lazy="true" >
    <id name="Id" column="ID" >
      <generator class="increment"> </generator>
    </id>
    <discriminator column="Degree"/>

    <property name="FirstName" column="FIRSTNAME" />
    <property name="LastName" column="LASTNAME" />
    <!--<property name="Degree" column="DEGREE" />-->
    <property name="Field" column="FIELD" />

    <subclass name="MasterStudent" discriminator-value="1">
         <property name="َArticle"  />
    </subclass>
    <subclass name="BachelorStudent" discriminator-value="2">

    </subclass>

    <set name="StudentCourses" table="StudentCourse" inverse="true" cascade="all,delete-orphan">
      <key column="ID"/>
      <one-to-many class="StudentCourse"/>
    </set>

  </class>
</hibernate-mapping>

我在文件映射“设置”中使用了关联标记

我的孩子班级是:

public class BachelorStudent : StudentDao
    {
    }
}

和下一个子类:

public class MasterStudent : StudentDao
    {
        public virtual bool Article { get; set; }
    }
}

运行此错误后,出现以下错误:

  

“名称空间'urn:nhibernate-mapping-2.2'中的元素'class'在名称空间'urn:nhibernate-mapping-2.2'中具有无效的子元素'set'。可能的元素列表:'subclass,loader, sql-insert,sql-update,sql-delete,filter,resultset,query,sql-query在'urn:nhibernate-mapping-2.2'命名空间中。”

我该怎么办?

1 个答案:

答案 0 :(得分:0)

set元素需要在子类元素之前定义。即:

<set name="StudentCourses" table="StudentCourse" inverse="true" cascade="all,delete-orphan">
  <key column="ID"/>
  <one-to-many class="StudentCourse"/>
</set>

<subclass name="MasterStudent" discriminator-value="1">
     <property name="َArticle"  />
</subclass>
<subclass name="BachelorStudent" discriminator-value="2">

</subclass>