代码之家  ›  专栏  ›  技术社区  ›  David Veeneman

nHibernate.DuplicateMapping异常

  •  0
  • David Veeneman  · 技术社区  · 15 年前

    我得到了一个 nHibernate.DuplicateMapping异常 我不明白。这个应用程序是一个简单的项目经理。它包含一个Project类,该类具有ProjectNote类型的Notes属性。以下是错误消息:

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

    类定义和映射文件如下所示。

    有人能告诉我为什么会出现重复映射异常吗?谢谢。


    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 回复  |  直到 15 年前
        1
  •  1
  •   John Rayner    15 年前

    您已经映射了两次ProjectNotes表。一次进入项目。将notes包作为projectnote类的主表。尝试从映射project.notes的元素中删除table属性。