开发者

NHibernate Duplicate class/entity mapping problem

I've have started my foray into C#.NET and NHibernate and I'm finally stuck on an exception I can't seem to figure out, and Google isn't helping.

I'm getting a "NHibernate.DuplicateMappingException : Duplicate class/entity mapping" on my Parent class. Below is my mapping file for the Parent class, and the Youth 开发者_运维技巧class that uses the Parent class:

    <?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>

And here is the relevant part of the Youth class (it is considerably bigger)

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

Only other thing is the Youth class also has the firstName and lastName properties, but I can't see that being a problem.


Make sure you are not doing both of these 2 things.

(1) adding the assembly in code:

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

(2) And then also adding the assembly in the config file:

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


You are adding the file or assembly containing the mapping twice to your Configuration object.


I had this problem, and solved it by putting this statement in hibernate.cfg.xml file:

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


Another possible cause to generate this error is multiple hbm files referencing the same Assembly during a Configuration.AddAssembly.

All hbm files in the same assembly are processed with one AddAssembly call.


As it gives a duplicate class entity mapping I can only imagen that you have two or more *.xml.hbm files referring to the same .net class.

Make sure that the xml class element for your Youth class does not have the same value for the name attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜