2010-09-12 197 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調用進行處理。