NHibernate重复类/实体映射问题

时间:2010-09-12 03:15:52

标签: c# nhibernate

我已经开始涉足C#.NET和NHibernate,我终于陷入了一个我无法理解的例外情况,谷歌没有帮助。

我得到了一个N​​Hibernate.DuplicateMappingException:我父类上的重复类/实体映射。下面是我的父类的映射文件,以及使用Parent类的Youth类:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Surrix.Cerberus.YouthData"
                   namespace="Surrix.Cerberus.YouthData.Domain">
  <class name="Parent">
    <id name="parentId">
      <generator class="guid" />
    </id>
    <property name="firstName" not-null="true" />
    <property name="lastName" not-null="true" />
    <property name="homePhone" />
    <property name="parentEmail" />
    <property name="relationshipToYouth" />

    <!-- Address component that should map to the Address class -->
    <component name="parentAddress">
      <property name="street" />
      <property name="state" />
      <property name="zipCode" />
      <property name="city" />
    </component>

  </class>

</hibernate-mapping>

以下是青年班的相关部分(相当大)

<set name="YouthParents" table="YouthParents" cascade="none">
  <key column="youthId" />
  <many-to-many column="parentId" class="Parent"/>
</set>

只有其他东西是Youth类也有firstName和lastName属性,但我看不出这是一个问题。

5 个答案:

答案 0 :(得分:14)

确保你 这两件事

(1)在代码中添加程序集

// Code Configuration
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Employee).Assembly); 
// Presuming Employee resides in "MyAssembly" as seen below.

(2)然后还在配置文件中添加程序集

<!-- .config configuration -->
<session-factory>
     <!-- bunch of other stuff -->
    <mapping assembly="MyAssembly"/> <!-- as in MyAssembly.dll -->
</session-factory>

答案 1 :(得分:7)

您正在将包含映射的文件或程序集两次添加到Configuration对象。

答案 2 :(得分:2)

我遇到了这个问题,并通过将此语句放在hibernate.cfg.xml文件中来解决它:

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

答案 3 :(得分:1)

生成此错误的另一个可能原因是在Configuration.AddAssembly期间引用同一程序集的多个hbm文件。

使用一个AddAssembly调用处理同一程序集中的所有hbm文件。

答案 4 :(得分:0)

因为它提供了一个重复的类实体映射,所以我只能想象你有两个或多个* .xml.hbm文件引用相同的.net类。

确保Youth类的xml类元素与name属性的值不相同。