表Y中的关联指的是未映射的类:Y - C#NHibernate

时间:2012-04-26 05:14:13

标签: nhibernate nhibernate-mapping

我得到了例外:

"An association from the table order refers to an unmapped class: FrancosPoS.DBMapping.pastaCombo"

这个NHibernate映射:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="order" table="order" lazy="true" >
    <id name="idOrder">
      <generator class="identity" />
    </id>
    <many-to-one insert="false" update="false" lazy="false" name="idPastaI" class="FrancosPoS.DBMapping.pastaIndividual">
      <column name="idPastaI" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <!--<property name="idPastaI">
      <column name="idPastaI" sql-type="int(11)" not-null="false" />
    </property>-->
    <many-to-one insert="false" update="false" lazy="false" name="pastaCombo" class="FrancosPoS.DBMapping.pastaCombo">
      <column name="idPastaC" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <!--<property name="idPastaC">
      <column name="idPastaC" sql-type="int(11)" not-null="false" />
    </property>-->
    <many-to-one insert="false" update="false" lazy="false" name="idPastaF" class="FrancosPoS.DBMapping.pastaFeast">
      <column name="idPastaF" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <!--<property name="idPastaF">
      <column name="idPastaF" sql-type="int(11)" not-null="false" />
    </property>-->
    <many-to-one insert="false" update="false" lazy="false" name="idSalad" class="FrancosPoS.DBMapping.salad">
      <column name="idSalad" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <!--<property name="idSalad">
      <column name="idSalad" sql-type="int(11)" not-null="false" />
    </property>-->
    <many-to-one insert="false" update="false" lazy="false" name="idDrink" class="FrancosPoS.DBMapping.drink">
      <column name="idDrink" sql-type="int(11)" not-null="false" />
    </many-to-one>
    <!--<property name="idDrink">
      <column name="idDrink" sql-type="int(11)" not-null="false" />
    </property>-->
    <property name="price">
      <column name="price" sql-type="decimal(8,4)" not-null="true" />
    </property>
    <property name="cash">
      <column name="cash" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="credit">
      <column name="credit" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="obs">
      <column name="obs" sql-type="varchar(150)" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

这是我的cs课程:

namespace FrancosPoS.DBMapping {

    public partial class order {
        public order() { }
        public virtual int idOrder { get; set; }
        public virtual pastaIndividual pastaIndividual { get; set; }
        public virtual pastaCombo pastaCombo { get; set; }
        public virtual pastaFeast pastaFeast { get; set; }
        public virtual salad salad { get; set; }
        public virtual drink drink { get; set; }
        public virtual string price { get; set; }
        public virtual System.Nullable<int> cash { get; set; }
        public virtual System.Nullable<int> credit { get; set; }
        public virtual string obs { get; set; }
    }
}

pastaCombo mapping:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FrancosPoS" namespace="FrancosPoS.DBMapping" xmlns="urn:nhibernate-mapping-2.2">
  <class name="pastaCombo" table="pasta_combo" lazy="true" >
    <id name="idPastaC">
      <generator class="identity" />
    </id>
    <property name="type">
      <column name="type" sql-type="varchar(25)" not-null="true" />
    </property>
    <property name="price">
      <column name="price" sql-type="decimal(8,4)" not-null="true" />
    </property>
    <property name="freeDrink">
      <column name="freeDrink" sql-type="smallint(5) unsigned" not-null="true" />
    </property>
    <property name="freeSalad">
      <column name="freeSalad" sql-type="smallint(5) unsigned" not-null="true" />
    </property>
  </class>
</hibernate-mapping>

pastaCombo课程:

namespace FrancosPoS.DBMapping {

    public class pastaCombo {
        public pastaCombo() { }
        public virtual int idPastaC { get; set; }
        public virtual string type { get; set; }
        public virtual string price { get; set; }
        public virtual int freeDrink { get; set; }
        public virtual int freeSalad { get; set; }
    }
}

我错过了什么?

此致

PS:属性被注释,因为我(认为我)不需要它们,因为我的类具有对象类型,而不是int ID。如果我使用它NHibernate抛出“试图通过反射设置属性类型”的错误。

编辑:包括pastaCombo地图和课程。

3 个答案:

答案 0 :(得分:8)

我刚刚遇到了同样的错误:

An association from the table <table name> refers to an unmapped class: <my entity class>

解决我的问题的唯一解决方案是在映射文件中添加.hbm,文件名应采用以下格式:<filename>.hbm.xml

答案 1 :(得分:6)

那么你在某处映射了FrancosPoS.DBMapping.pastaCombo吗?是多对一,它需要引用映射的实体类型。

您对pastaCombo的映射是否实际包含在程序集中(如果使用嵌入式资源)并且程序集是否已加载到会话工厂?

答案 2 :(得分:4)

我可以看到这个问题有一个公认的答案,但我想补充一些东西。我花了半天的时间试图理解为什么会抛出这个异常。我已经搜索了十几个答案,但我无法找出问题所在。我在我的项目中集成了NHibernate,观看了this帖子。在我的Models文件夹中,我创建了一个内部文件夹NHibernate,在其中我创建了一个文件夹Configuration,其中包含hibernate.cfg.xml文件和一个Mappings文件夹,其中我的实体的所有映射都位于文件下喜欢这个EntityName.hbm.xml。

问题在于,在我的多对一和一对多映射中,在class属性中我只有实体类名。例如,class =&#34; Category&#34;。这条路不够!!!我已经看过this帖子,它有一个短语&#34;完全合格的名字&#34;。这让我改变了类=&#34;类别&#34; to class =&#34; myProject.Models.Category&#34;所以它可以找到Category类。

希望这有帮助!