2010-09-12 199 views
4

我已经开始进入C#.NET和NHibernate,我终于停留在我似乎无法解决的异常上,而Google并没有提供帮助。NHibernate重复类/实体映射问题

我得到一个NHibernate.DuplicateMappingException:在我的父类重复类/实体映射。下面是使用父类的我的映射文件的父类和青少年类:

<?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属性,但我看不出这是一个问题。

回答

7

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

+0

这听起来像它可能是正确的。目前,我正在加载组件中的配置对象,并在单元测试类中创建另一个,以便我可以导出该模式。有没有不同的方式来处理这个问题? – 2010-09-13 15:21:07

+0

您应该在一个地方构建您的配置,但问题在于您添加映射的方式。你应该发布代码。 – 2010-09-13 15:48:50

+0

虽然这个答案没有完全让我达到最终结果,但它确实让我走上了这条路。如果我发布了其余的HBM,我相信它会被发现。在青少年时期,我参考了家长和其他班级。所以当你将组件添加到Configuration对象时,你只需要添加Youth对象。至少这是解决我的问题。 – 2010-09-17 23:40:44

0

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

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

14

确保你是不是这2件事情。

(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> 
+0

这个答案的一些额外信息[here](http://elliottjorgengen.com/nhibernate-api-ref/NHibernate.Cfg/Configuration.html)。我最终使用'cfg.AddAssembly(Assembly.GetExecutingAssembly())'解决了我的问题。 – justanotherdev 2014-09-15 13:25:22

2

我有这个问题,并通过在hibernate.cfg.xml文件中加以解决:

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

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

同一个程序集中的所有hbm文件都通过一个AddAssembly调用进行处理。