简体   繁体   English

在Nhibernate 2.1中复制子实体吗?

[英]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 1.2迁移到Nhibernate 2.1,但遇到了一个奇怪的错误。 NHibernate is loading duplicate copies of child entities into collection properties. NHibernate正在将子实体的重复副本加载到集合属性中。 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. 这里是问题的详细信息:我的项目是一个简单的Project Manager。 The three entities of interest are Project, Task, and ProjectNote. 感兴趣的三个实体是Project,Task和ProjectNote。 A Project has two collection properties, Tasks and Notes, to hold Task and ProjectNote objects. 一个项目有两个集合属性,任务和便笺,用于保存任务和ProjectNote对象。 My test data has several projects. 我的测试数据有几个项目。 Project #1 has 4 Tasks and 2 Notes. 项目1具有4个任务和2个注释。 But when Project #1 is loaded, it shows 10 Tasks and 10 Notes, with repetition in each collection. 但是,在加载项目#1时,它将显示10个任务和10个便笺,每个集合中都有重复项。 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: 首先,我的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
    }
}

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: 还有我的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
    }
}

Here is the Project.hmb.xml file: 这是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>

The Task.hbm.xml file: 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>

And my ProjectNote.hbm.xml file: 还有我的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>

I'm not sure what the fix is, but I think the issue is that you have your 2 collections marked as "fetch='join'". 我不确定解决方案是什么,但我认为问题在于您将2个集合标记为“ fetch ='join'”。 The SQL that would be generated would return 10 rows. 将生成的SQL将返回10行。 I guess quick fix would be "fetch='select'" on the collections. 我想快速解决方案是在集合上“ fetch ='select'”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM