Hello all !!
I have this model:
[Master]---<[Slave]>---[Other]
I have a master and I want to save a new slave and other in cascade. So I set cascade="all-delete-orphan" on Master.SlaveSet (1ToN) and Slave.Other (NTo1) in the hbm files.
The main code is this:
Code:
Master master = new Master();
master.IdMaster = 3;
Slave slave = new Slave();
slave.IdSlave = 0;
Other other = new Other();
other.IdOther = 0;
master.SlaveSet = new Iesi.Collections.HashedSet();
master.SlaveSet.Add(slave);
slave.Master = master;
other.SlaveSet = new Iesi.Collections.HashedSet();
other.SlaveSet.Add(slave);
slave.Other = other;
object returnObject = session.SaveOrUpdateCopy(master);
The error generated is this:
Code:
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - processing cascades for: MasterSlave.Master
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - cascading to collection: MasterSlave.Master.SlaveSet
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - cascading to Copy()
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - unsaved-value: 0
DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - saving [MasterSlave.Slave#<null>]
DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - executing insertions
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - processing cascades for: MasterSlave.Slave
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - cascading to Copy()
DEBUG NHibernate.Engine.Cascades [(null)] <(null)> - unsaved-value: 0
DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - saving [MasterSlave.Other#<null>]
DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - executing insertions
DEBUG NHibernate.Impl.WrapVisitor [(null)] <(null)> - Wrapped collection in role: MasterSlave.Other.SlaveSet
DEBUG NHibernate.Persister.EntityPersister [(null)] <(null)> - Inserting entity: MasterSlave.Other (native id)
DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Opened new IDbCommand, open IDbCommands :1
DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Building an IDbCommand object for the SqlString: INSERT INTO Other (Description) VALUES (:Description); select SCOPE_IDENTITY()
DEBUG NHibernate.Persister.EntityPersister [(null)] <(null)> - Dehydrating entity: [MasterSlave.Other#<null>]
DEBUG NHibernate.Type.NullableType [(null)] <(null)> - binding '(New) Other Description' to parameter: 0
DEBUG NHibernate.SQL [(null)] <(null)> - INSERT INTO Other (Description) VALUES (@p0); select SCOPE_IDENTITY()
DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Opened Reader, open Readers :1
DEBUG NHibernate.Persister.AbstractEntityPersister [(null)] <(null)> - Natively generated identity: 24
DEBUG NHibernate.Driver.NHybridDataReader [(null)] <(null)> - running NHybridDataReader.Dispose()
DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed Reader, open Readers :0
DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed IDbCommand, open IDbCommands :0
ERROR NHibernate.AssertionFailure [(null)] <(null)> - An AssertionFailure occured - this may indicate a bug in NHibernate
NHibernate.AssertionFailure: cannot cache a reference to an object with a null id: Slave
DEBUG NHibernate.Util.ADOExceptionReporter [(null)] <(null)> - Could not save object
The Hbm files are:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MasterSlave.Master, MasterSlave" table="Master">
<id
column="IdMaster"
name="IdMaster"
type="integer"
unsaved-value="0">
<generator class="native" />
</id>
<property
column="Description"
name="Description"
not-null="false"
type="string"/>
<set
inverse="true"
lazy="true"
name="SlaveSet"
cascade="all-delete-orphan">
<key column="FkMaster" />
<one-to-many class="MasterSlave.Slave, MasterSlave" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MasterSlave.Slave, MasterSlave" table="Slave">
<id
column="IdSlave"
name="IdSlave"
type="integer"
unsaved-value="0">
<generator class="native" />
</id>
<property
column="Description"
name="Description"
not-null="false"
type="string"/>
<many-to-one
class="MasterSlave.Master, MasterSlave"
name="Master"
not-null="true">
<column name="FkMaster"/>
</many-to-one>
<many-to-one
class="MasterSlave.Other, MasterSlave"
name="Other"
not-null="true"
cascade="all-delete-orphan">
<column name="FkOther"/>
</many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MasterSlave.Other, MasterSlave" table="Other">
<id
column="IdOther"
name="IdOther"
type="integer"
unsaved-value="0">
<generator class="native" />
</id>
<property
column="Description"
name="Description"
not-null="false"
type="string"/>
<set
inverse="true"
lazy="true"
name="SlaveSet">
<key column="FkOther"/>
<one-to-many class="MasterSlave.Slave, MasterSlave"/>
</set>
</class>
</hibernate-mapping>
Is it possible to do?
When I only use One-To-Many relations the cascade saving works perfectly.
For example:
[A]---<[B]---<[C] ...
Thanks !!