NHibernate : delete error

Posted by MadSeb on Stack Overflow See other posts from Stack Overflow or by MadSeb
Published on 2010-04-27T17:12:48Z Indexed on 2010/04/27 17:13 UTC
Read the original article Hit count: 281

Hi,

Model: I have a model in which one Installation can contain multiple "Computer Systems".

Database: The table Installations has two columns Name and Description. The table ComputerSystems has three columsn Name, Description and InstallationId.

Mappings:

I have the following mapping for Installation:

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

I have the following mapping for ComputerSystem:

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

Classes:

The Installation class is:

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= "";
    }


    }

The ComputerSystem class is:

public class ComputerSystem { public virtual String Name { get; set; } public virtual String Description { get; set; } public virtual Installation Installation { get; set; }

}

The issue is that I get an error when trying to delete an installation that contains a ComputerSystem. The error is: "deleted object would be re-saved by cascade (remove deleted object from associations)". Can anyone help ?

Regards, Seb

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about database