-->
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.  [ 2 posts ] 
Author Message
 Post subject: Cascade delete and not-null constraint problem
PostPosted: Thu Jun 23, 2005 3:52 am 
Newbie

Joined: Thu Jun 23, 2005 3:25 am
Posts: 1
Hey all,

I've got a strange problem with nh, and need some fresh eyes after 3 hours of trying to fix it!

I have a Quote which contains many Parties.
Code:
   <class name="JobTrak.Domain.Persistable.Quote, JobTrak.Domain" table="Quotes" lazy="true">
      <id name="Id" column="QuoteID" type="Int32" unsaved-value="-1">
         <generator class="identity"/>
      </id>
      
       <property name="QuoteNumber" type="Int32" not-null="true"/>
      <property name="QuotedDate" type="DateTime" not-null="true"/>

      <bag name="Parties" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="QuoteID"/>
         <one-to-many class="JobTrak.Domain.Persistable.QuoteParty, JobTrak.Domain"/>
      </bag>      

      <bag name="Items" inverse="true" cascade="all-delete-orphan">
         <key column="QuoteID"/>
         <one-to-many class="JobTrak.Domain.Persistable.QuotedItem, JobTrak.Domain"/>
      </bag>      
   </class>


and a Party is defined as:


Code:
   <class name="JobTrak.Domain.Persistable.QuoteParty, JobTrak.Domain" table="QuoteParties">
      <id name="Id" column="QuotePartyID" type="Int32" unsaved-value="-1">
         <generator class="identity"/>
      </id>

      <many-to-one name="Party" column="PartyID" class="JobTrak.Domain.Persistable.Party, JobTrak.Domain" not-null="true"/>      
      <many-to-one name="Role" column="PartyRoleID" class="JobTrak.Domain.Persistable.PartyRole, JobTrak.Domain" not-null="true"/>
      <many-to-one name="Quote" column="QuoteID" class="JobTrak.Domain.Persistable.Quote, JobTrak.Domain" not-null="true"/>
   </class>


To remove a Party from the Quote I invoke a method on the Quote object:
Code:
      public void RemoveParty(QuoteParty party)
      {
         for (int i = 0; i < Parties.Count; i++)
         {
            if ((Parties[i] == party && party.Id == -1) || (party.Id > -1 && ((QuoteParty)Parties[i]).Id == party.Id))
            {
               QuoteParty qp = (QuoteParty) Parties[i];
               Parties.RemoveAt(i);
               qp.Quote = null;
               break;
            }
         }
      }



Testing the Quote object alone (without persistence) works: Parties are removed as requested.

Persisting the Quote (and QuoteParties via cascade) also works.

I then reload the Quote and Initialise the Parties collection, remove a party from the Quote and re-save the quote. Once I attempt to resave the quote I get the exception "not-null property references a null or transient value: QuoteParty, Quote".

I'm absolutely stumped as to why. Performing the same operation on the other collection named Items causes no problems.

From what I can see it should work. Even according to the log it seems it should work:
2005-06-23 17:41:38,714 [5144] NHibernate.Impl.SessionImpl..ctor(:0) - opened session
2005-06-23 17:41:38,724 [5144] NHibernate.Engine.Cascades+IdentifierValue.IsUnsaved(:0) - unsaved-value: -1
2005-06-23 17:41:38,724 [5144] NHibernate.Impl.SessionImpl.SaveOrUpdate(:0) - SaveOrUpdate() previously saved instance with id: 1
2005-06-23 17:41:38,734 [5144] NHibernate.Impl.SessionImpl.DoUpdateMutable(:0) - updating [JobTrak.Domain.Persistable.Quote#1]
2005-06-23 17:41:38,734 [5144] NHibernate.Engine.Cascades.Cascade(:0) - processing cascades for: JobTrak.Domain.Persistable.Quote
2005-06-23 17:41:38,734 [5144] NHibernate.Engine.Cascades.CascadeCollection(:0) - cascading to collection: JobTrak.Domain.Persistable.Quote.Items
2005-06-23 17:41:38,744 [5144] NHibernate.Engine.Cascades.CascadeCollection(:0) - cascading to collection: JobTrak.Domain.Persistable.Quote.Parties
2005-06-23 17:41:38,744 [5144] NHibernate.Engine.Cascades+CascadingAction+ActionSaveUpdateClass.Cascade(:0) - cascading to SaveOrUpdate()
2005-06-23 17:41:38,754 [5144] NHibernate.Engine.Cascades+IdentifierValue.IsUnsaved(:0) - unsaved-value: -1
2005-06-23 17:41:38,754 [5144] NHibernate.Impl.SessionImpl.SaveOrUpdate(:0) - SaveOrUpdate() previously saved instance with id: 1
2005-06-23 17:41:38,764 [5144] NHibernate.Impl.SessionImpl.DoUpdateMutable(:0) - updating [JobTrak.Domain.Persistable.QuoteParty#1]
2005-06-23 17:41:38,764 [5144] NHibernate.Impl.SessionImpl.Delete(:0) - deleting a transient instance
2005-06-23 17:41:38,774 [5144] NHibernate.Impl.SessionImpl.DoDelete(:0) - deleting [JobTrak.Domain.Persistable.QuoteParty#2]
2005-06-23 17:41:38,784 [5144] NHibernate.Engine.Cascades+IdentifierValue.IsUnsaved(:0) - unsaved-value: -1
2005-06-23 17:41:38,784 [5144] NHibernate.Engine.Cascades+IdentifierValue.IsUnsaved(:0) - unsaved-value: -1
2005-06-23 17:41:38,794 [5144] NHibernate.Impl.SessionImpl.Close(:0) - closing session


The test code:
Code:
   Quote quote = PrepareQuote();

   quoteService.SaveQuote(quote);

   Quote loaded = quoteService.LoadQuote(quote.Id);
   Assert.AreEqual(2, loaded.Parties.Count);

   QuoteParty party = (QuoteParty) loaded.Parties[1];
   loaded.RemoveParty(party);

   Assert.AreEqual(1, loaded.Parties.Count);

   quoteService.SaveQuote(loaded);


Does anybody know what I'm missing?

Thanks in advance,
Sean.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 6:50 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well, it happens because RemovedParty.Quote is null and you have it mapped as not-null and NHibernate does a not-null check even when deleting the object. But this is of course a bug, I'll create a JIRA issue for it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.