在Nhibernate 2.1中复制子实体?

时间:2010-01-06 17:19:20

标签: nhibernate

我正在将项目从NHibernate 1.2迁移到Nhibernate 2.1,我遇到了一个奇怪的错误。 NHibernate正在将子实体的重复副本加载到集合属性中。有没有其他人遇到这个问题,你能建议修复吗?谢谢你的帮助。

以下是问题详情:我的项目是一个简单的项目经理。感兴趣的三个实体是Project,Task和ProjectNote。项目有两个集合属性,任务和注释,用于保存Task和ProjectNote对象。我的测试数据有几个项目。项目#1有4个任务和2个注释。但是当加载Project#1时,它会显示10个任务和10个Notes,并在每个集合中重复。如果我加载任何其他项目,我会得到相同的结果。

以下是类和映射文件。首先,我的Project类:

using System.Collections.Generic;

namespace FsProjectManager.Common.Domain
{
    public class Project
    {
        #region Constructor

        public Project()
        {
            Initialize();
        }

        #endregion

        #region Properties

        public virtual int ID { get; set; }

        public virtual int Index { get; set; }

        public virtual string Name { get; set; }

        public virtual IList<ProjectNote> Notes { get; set; }

        public virtual IList<Task> Tasks { get; set; }

        #endregion

        #region Private Methods

        private void Initialize()
        {
            Tasks = new List<Task>();
            Notes = new List<ProjectNote>();
        }

        #endregion
    }
}

我的任务对象:

using System;
using System.Collections.Generic;

namespace FsProjectManager.Common.Domain
{
    public class Task
    {
        #region Constructor

        public Task()
        {
            Initialize();
        }

        #endregion

        #region Properties

        public virtual int ID { get; set; }

        public virtual int SequenceNumber { get; set; }

        public virtual bool Completed { get; set; }

        public virtual string Description { get; set; }

        public virtual DateTime DueDate { get; set; }

        public virtual IList<TaskNote> Notes { get; set; }

        public virtual int NumDays { get; set; }

        public virtual Project Parent { get; set; }

        #endregion

        #region Private Methods

        private void Initialize()
        {
            Notes = new List<TaskNote>();
        }

        #endregion

    }
}

我的ProjectNote类:

namespace FsProjectManager.Common.Domain
{
    public class ProjectNote
    {
        #region Properties

        public virtual int ID { get; set; }

        public virtual Project Parent { get; set; }

        public virtual string Text { get; set; }

        #endregion
    }
}

这是Project.hmb.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="true"
                   assembly="FsProjectManager.Common"
                   namespace="FsProjectManager.Common.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" fetch="join">
      <key column="ProjectID" />
      <one-to-many class="Task" />
    </bag>

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


  </class>

</hibernate-mapping>

Task.hbm.xml文件:

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

  <!-- Map class 'Task' -->
  <class name="Task" table="Tasks">

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

    <!-- Simple properties -->
    <property name="Description" column="Description" type="String" />
    <property name="DueDate" column="DueDate" type="DateTime" />
    <property name="NumDays" column="NumDays" type="Int32" />

    <!-- Collection properties: Parent-side -->
    <bag name="Notes" table="TaskNotes" cascade="all-delete-orphan" inverse="true" fetch="join">
      <key column="TaskID" />
      <one-to-many class="TaskNote" />
    </bag>

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

  </class>

</hibernate-mapping>

我的ProjectNote.hbm.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   auto-import="true"
                   assembly="FsProjectManager.Common"
                   namespace="FsProjectManager.Common.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" fetch="join" />

  </class>

</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

我不确定修复是什么,但我认为问题在于您将2个集合标记为“fetch ='join'”。将生成的SQL将返回10行。我想快速解决方案是集合上的“fetch ='select'”。