views:

15

answers:

1

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

A: 

I think this is caused by the cascade="save-update" in the mapping file for ComputerSystem. If you don't need to cascade in that direction (child to parent) then you may be able to remove it.

Alternatively, you could try clearing the installation object's ComputerSystems list before deleting the instalation.

Daniel Renshaw