tags:

views:

282

answers:

1

I'm getting an NHibernate.DuplicateMappingException that I don't understand. The app is a simple project manager. It contains a Project class, which has a Notes property of type ProjectNote. Here is the error message:

NHibernate.MappingException: Could not compile the mapping document: ProjectManager.Domain.Mapping.ProjectNote.hbm.xml ---> NHibernate.DuplicateMappingException: Duplicate class/entity mapping ProjectNote

The class definitions and mapping files are reproduced below.

Can anybody tell me why I am getting a duplicate mapping exception? Thanks.


using System.Collections.Generic;

namespace ProjectManager.Domain
{
    public class Project
    {
        #region Constructor

        public Project()
        {
            Initialize();
        }

        #endregion

        #region Properties

        /// <summary>
        /// The ID of this project.
        /// </summary>
        public virtual int ID { get; set; }

        /// <summary>
        /// The value used to sort this item in a Projects list.
        /// </summary>
        public virtual int Index { get; set; }

        /// <summary>
        /// The name of this project.
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// Notes for this project.
        /// </summary>
        public virtual IList<ProjectNote> Notes { get; set; }

        /// <summary>
        /// The tasks in this project.
        /// </summary>
        public virtual IList<Task> Tasks { get; set; }

        #endregion

        #region Private Methods

        /// <summary>
        /// Initializes this class.
        /// </summary>
        private void Initialize()
        {
            Tasks = new List<Task>();
            Notes = new List<ProjectNote>();
        }

        #endregion
    }
}


namespace ProjectManager.Domain
{
    public class ProjectNote
    {
        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public virtual int ID { get; set; }

        /// <summary>
        /// The parent project of this note.
        /// </summary>
        public virtual Project Parent { get; set; }

        /// <summary>
        /// The text of the note
        /// </summary>
        public virtual string Text { get; set; }

        #endregion
    }
}


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="true"
                   assembly="ProjectManager.Domain"
                   namespace="ProjectManager.Domain">

  <!-- Map class 'Project' -->
  <class name="Project" table="Projects">

    <!-- Identifier column -->
    <id name="ID" column="ID" type ="Int32" unsaved-value="0">
      <generator class="native" />
    </id>

    <!-- Simple properties -->
    <property name="Name" column="Name" type="String" not-null="true" />

    <!-- Collection properties: Parent-side -->
    <bag name="Tasks" table="Tasks" cascade="all-delete-orphan" inverse="true">
      <key column="ProjectID" />
      <one-to-many class="Task" />
    </bag>

    <bag name="Notes" table="ProjectNotes" cascade="all-delete-orphan" inverse="true">
      <key column="ProjectID" />
      <one-to-many class="ProjectNote" />
    </bag>


  </class>

</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="true"
                   assembly="ProjectManager.Domain"
                   namespace="ProjectManager.Domain">

  <!-- Map class 'ProjectNote' -->
  <class name="ProjectNote" table="ProjectNotes">

    <!-- Identifier column -->
    <id name="ID" column="ID" type ="Int32" unsaved-value="0">
      <generator class="native" />
    </id>

    <!-- Simple properties -->
    <property name="Text" column="Text" type="String" />

    <!-- Collection properties: Child-side -->
    <many-to-one name="Parent" column="ProjectID" class="Project" />

  </class>

</hibernate-mapping>
+1  A: 

You've mapped the ProjectNotes table twice. Once into the Project.Notes bag and again as the main table for the ProjectNote class. Try removing the table attribute from your element that maps Project.Notes.

John Rayner
Thanks, John! I'll try that.
David Veeneman
After doing some further testing, I'm not sure that was the cause. I removed the ProjectNote mapping file from the project and the exception disappeared. Later, I re-added the file, expecting to reproduce the exception, but didn't get it. This one is still a bit of a mystery. I'll leave John's answer marked as the correct one, because he may be right. But I can't vouch for the solution 100%.
David Veeneman