NHibernate:表X中的关联引用了一个未映射的类:X.

时间:2011-12-12 23:22:15

标签: c# nhibernate nhibernate-mapping

我有一个订单和客户对象的两个映射文件。

客户映射:          

  <class name="OODB.Domain.Customer, OODB.Domain" entity-name="Customers">
     <id name="ID" column="customer_id">
      <generator class="guid" />
     </id>
     <property name="FirstName" column="first_name"/>
     <property name="LastName" column="last_name"/>
     <property name="EMail" column="email"/>
     <property name="Telephone" column="telephone" />

    <component name="Address" class="Address">
       <property name="Street" column="street"></property>
       <property name="PostalCode" column="postal_code"></property>
       <property name="City" column="city"></property>
    </component>
  </class>
 </hibernate-mapping>

订单映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OODB.Domain"   namespace="OODB.Domain">  
  <class name="OODB.Domain.CustomerOrder, OODB.Domain" entity-name="Orders">
    <id name="ID" column="order_id">
      <generator class="guid"></generator>
    </id>
    <property name="OrderNo" column="order_no" length="8" not-null="true"></property>
    <property name="Status" column="status" not-null="true"></property>
    <many-to-one name="Orderer" class="Customer" column="customer_id" insert="true" not-found="exception" fetch="join"/>
  </class>
</hibernate-mapping>

客户类:

namespace OODB.Domain
{
    public class Customer : ModelBase<Customer>
    {
        public virtual string FirstName
        {
            get;
            set;
        }

        public virtual string LastName
        {
            get;
            set;
        }

        public virtual string EMail
        {
            get;
            set;
        }

        public virtual string Telephone
        {
            get;
            set;
        }

        public virtual Address Address
        {
            get;
            set;
        }

...
}

客户订单类:

public class CustomerOrder : ModelBase<CustomerOrder>
{
    public virtual string OrderNo
    {
        get;
        set;
    }

    public virtual Customer Orderer
    {
        get;
        set;
    }

    public virtual OrderStatus Status
    {
        get;
        set;
    }
...
}

如果我删除多对一的东西(映射被集成为嵌入式资源,那么一切都很好。我已经检查了两次。)。否则我收到错误'表中的关联订单引用未映射的类:OODB.Domain.Customer'。但Customer对象已映射...我缺少什么?

4 个答案:

答案 0 :(得分:4)

我猜你的例子:many-to-one元素中的“class”规范必须与其他class规范具有相同的详细程度。特别是,您必须包括程序集。

<many-to-one name="Orderer" class="OODB.Domain.Customer, OODB.Domain" column="customer_id" insert="true" not-found="exception" fetch="join"/>

答案 1 :(得分:2)

我在class元素上混合了table和entity-name属性。 :\

答案 2 :(得分:1)

此异常的可能来源是.html文件不是“嵌入式资源”。如果映射文件(hbm)不在正确的目录中,则此异常。如果你看一下输出窗口,你会看到类似的东西:

NHibernate.Cfg.Configuration: ERROR lambda_method - An association from the table
SubsequentReportDetail refers to an unmapped class: NS.Project.Data.Domain.Class
NHibernate.MappingException: An association from the table X refers to an 
unmapped class:  NS.Project.Data.Domain.Class

答案 3 :(得分:1)

我的问题是..相同属性映射两次

   Property(x => x.MyProperty);
   Bag(x => x.MyProperty, colmap => { }, map => map.OneToMany(x => { }));