【问题标题】:NHibernate : delete errorNHibernate:删除错误
【发布时间】:2010-04-27 17:12:48
【问题描述】:

型号: 我有一个模型,其中一个安装可以包含多个“计算机系统”。

数据库: 表 Installations 有两列 Name 和 Description。 表 ComputerSystems 包含三列 Name、Description 和 InstallationId。

映射:

我有以下安装映射:

<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="myProgram.Core" namespace="myProgram">

  <class name="Installation" table="Installations" lazy="true">

    <id name="Id" column="Id" type="int">
      <generator class="native" />
    </id>

    <property name="Name" column="Name" type="string" not-null="true" />
    <property name="Description" column="Description" type="string" />


    <bag name="ComputerSystems" inverse="true" lazy="true" cascade="all-delete-orphan">
      <key column="InstallationId" />
      <one-to-many class="ComputerSystem" />
    </bag>

  </class>

</hibernate-mapping>

我有以下计算机系统的映射:

<?xml version="1.0" encoding="utf-8"?>

<id name="Id" column="ID" type="int">
  <generator class="native" />
</id>

<property name="Name" column="Name" type="string" not-null="true" />
<property name="Description" column="Description" type="string" />

<many-to-one name="Installation" column="InstallationID" cascade="save-update" not-null="true" />

类:

安装类是:

public class Installation 
{

    public virtual String Description { get; set; }
    public virtual String Name { get; set; } 


    public virtual IList<ComputerSystem> ComputerSystems
    { 
        get
        {
            if (_computerSystemItems== null)
            {
                lock (this)
                {
                    if (_computerSystemItems== null)
                        _computerSystemItems= new List<ComputerSystem>();
                }
            }
            return _computerSystemItems;
        } 
        set
        {
            _computerSystemItems= value; 
        }
    }

    protected IList<ComputerSystem> _computerSystemItems;




    public Installation()
    {
        Description = "";
        Name= "";
    }


    }

ComputerSystem 类是:

公共类ComputerSystem { 公共虚拟字符串名称 { 获取;放; } 公共虚拟字符串描述{get;放; } 公共虚拟安装安装{get;放; }

}

问题是我在尝试删除包含 ComputerSystem 的安装时遇到错误。错误是:“已删除的对象将由级联重新保存(从关联中删除已删除的对象)”。任何人都可以帮忙吗?

问候, 赛博

【问题讨论】:

    标签: database nhibernate cascading-deletes


    【解决方案1】:

    我认为这是由 ComputerSystem 映射文件中的 cascade="save-update" 引起的。如果您不需要在该方向级联(子级到父级),那么您可以将其移除。

    或者,您可以尝试在删除安装之前清除安装对象的计算机系统列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多