【问题标题】:NHibernate: An association from the table X refers to an unmapped class: XNHibernate:来自表 X 的关联引用了一个未映射的类:X
【发布时间】:2012-01-18 22:08:25
【问题描述】:

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

客户映射:

  <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;
    }
...
}

如果我删除多对一的东西,一切都很好(映射被集成为嵌入式资源。我已经检查了两次。)。否则,我会收到错误“来自表 Orders 的关联引用了一个未映射的类:OODB.Domain.Customer”。但是客户对象被映射了......我错过了什么?

【问题讨论】:

  • 也许向我们展示“CustomerOrder”类的实际类定义。具体来说,“订购者”属性。
  • 要测试的另一件事:您是否将“Customer.hbm.xml”上的“BuildAction”设置为“EmbeddedResource”? (这是一件简单的事情,过去让我很着迷)
  • 您是否设法自己获取客户对象?

标签: c# nhibernate nhibernate-mapping


【解决方案1】:

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

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

【讨论】:

  • 您是否对 Customer 映射中的 Address 类进行了同样的调整?
【解决方案2】:

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

【讨论】:

  • 你应该看看 Fluent nHibernate。它提供了一种更不容易出错的方式来定义映射。
  • 我需要在 .hbm 文件的属性构建操作中将其设置为嵌入资源
【解决方案3】:

此异常的可能来源是 .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

【讨论】:

    【解决方案4】:

    对我来说问题是......同一属性映射了两次

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多