-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Cascade saving error on one to many to one relation
PostPosted: Tue Oct 11, 2005 6:32 am 
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 !!


Top
  
 
 Post subject: Workaround solution
PostPosted: Mon Oct 17, 2005 6:28 am 
Newbie

Joined: Tue Oct 11, 2005 6:47 am
Posts: 1
I did not found solution, so this is the workaround I have used:

I set cascade="all-delete-orphan" on Master.SlaveSet (1ToN) and cascade="none" on Slave.Other (NTo1) in the hbm files.

And then I propagate the changes before running SaveOrUpdateCopy. The only one problem are orphan childs, because they are not deleted automatically.

To delete them, I use a IInterceptor class and this OnDelete method:
Code:
if (entity is Slave)
{
   Slave slave = (Slave) entity;
   if (slave.Other.SlaveSet.Count<2)
      _session.Delete(slave.Other);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 3:08 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:09 pm
Posts: 23
I just ran into this issue with a one-to-one mapping. Using SaveOrUpdate instead of SaveOrUpdateCopy fixed it for me, but I'm not exactly happy with that solution. Has anyone else had issues with this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.