tags:

views:

137

answers:

3

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 parts 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.

A: 

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.

Ramon
+2  A: 

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

Diego Mijelshon
This sounds like it might be right. Currently I'm loading the Configuration object in the assembly, and making another one inside my unit test class so I can export the schema. Is there a different way to handle this?
Patrick
You should build your configuration in a single place, but the problem is in the way you add the mappings. You should post the code for that.
Diego Mijelshon
While this answer didn't fully get me to an end result, it did set me along the path. Had I posted the rest of my HBMs I'm sure it would have been figured out. In the Youth hbm I reference the Parent, and other classes. So when you add your Assemblies to the Configuration object you only need to add the Youth object. At least that is what fixed my problem.
Patrick
A: 

Make sure you are not doing both of these:

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

And then

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