开发者

Duplicating child entities in Nhibernate 2.1?

I am migrating a project from NHibernate 1.2 to Nhibernate 2.1, and I have run into an odd error. NHibernate is loading duplicate copies of child entities into collection properties. Has anyone else run into this problem, and can you suggest a fix? Thanks for your help.

Here are the problem details: My project is a simple Project Manager. The three entities of interest are Project, Task, and ProjectNote. A Project has two collection properties, Tasks and Notes, to hold Task and ProjectNote objects. My test data has several projects. Project #1 has 4 Tasks and 2 Notes. But when Project #1 is loaded, it shows 10 Tasks and 10 Notes, with repetition in each collection. I get the same sort of result if I load any of the other projects.

Here are the classes and mapping files. First, my Project class:

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

My Task object:

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

    }
}

And my ProjectNote class:

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

Here is the Project.hmb.xml file:

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

The Task.hbm.xml file:

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

And my ProjectNote.hbm.xml file:

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


I'm not sure what the fix is, but I think the issue is that you have your 2 collections marked as "fetch='join'". The SQL that would be generated would return 10 rows. I guess quick fix would be "fetch='select'" on the collections.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜