-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-To-Many delete
PostPosted: Tue Oct 04, 2005 10:37 pm 
Newbie

Joined: Mon Jun 20, 2005 8:47 pm
Posts: 11
Hi,

Pulling my hair out over this one :o

If I understand this correctly, if I have a one to many mapping and I delete a child from the parent's collection, then it should be deleted in the database.

For example, my parent mapping:

Code:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="XXX.BusinessEntities" assembly="XXX">

   <class name="ComplianceCriteria" table="ComplianceCriteria"  dynamic-update="true">
       
        <id name="Id" column="CC_ID" type="Int32">
            <generator class="identity" />
        </id>

      <!-- Standard Audit Properties -->
      <version name="RowVersion" column="RowVersion" type="Int32" />
      <property name="CreationUser" column="CreationUser" type="String" />
         <property name="CreationDateTime" column="CreationDateTime" type="DateTime" />
         <property name="LastUpdateUser" column="LastUpdateUser" type="String" />
         <property name="LastUpdateDateTime" column="LastUpdateDateTime" type="DateTime" />
        <!-- End -->

        <!-- One-to-Many Collections -->
        <bag name="LetterTexts" table="ComplianceCriteriaLetterText" cascade="none" inverse="false" lazy="false">
         <key column="CLT_CC_ID" />
         <one-to-many class="ComplianceLetterText"></one-to-many>
      </bag>

   </class>
</hibernate-mapping>


and my child mapping:

Code:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="XXX.BusinessEntities" assembly="XXX">

   <class name="ComplianceLetterText" table="ComplianceCriteriaLetterText" dynamic-update="true">
       
        <id name="Id" column="CLT_ID" type="Int32">
            <generator class="identity" />
        </id>

      <!-- Standard Audit Properties -->
      <version name="RowVersion" column="RowVersion" type="Int32" />
      <property name="CreationUser" column="CreationUser" type="String" />
         <property name="CreationDateTime" column="CreationDateTime" type="DateTime" />
         <property name="LastUpdateUser" column="LastUpdateUser" type="String" />
         <property name="LastUpdateDateTime" column="LastUpdateDateTime" type="DateTime" />
        <!-- End -->

         <many-to-one name="ComplianceCriteria" column="CLT_CC_ID" insert="true" update="true" />
        
   </class>
</hibernate-mapping>


So if I execute the following for an existing row with a child LetterText:

Code:

ComplianceCriteria cc = (ComplianceCriteria)session.Load(cc.GetType(), 2);
cc.LetterTexts.RemoveAt(0);
session.BeginTransaction();
session.SaveOrUpdate(cc);
session.Transaction.Commit();



I get the following message:

Code:
"Cannot insert the value NULL into column 'CLT_CC_ID', table 'LgsDev.dbo.ComplianceCriteriaLetterText'; column does not allow nulls. UPDATE fails."


When working through the source code I see that the OneToManyPersister.GenerateDeleteRowString() creates this statement.

What have I missed? Should NHibernate be deleting this row for me or do I have to do it manually? If it can, then what do I need to change in my mapping to make this happen?

Thanks in advance,

Mike


Top
 Profile  
 
 Post subject: Re: One-To-Many delete
PostPosted: Wed Oct 05, 2005 8:14 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
michael1d wrote:
If I understand this correctly, if I have a one to many mapping and I delete a child from the parent's collection, then it should be deleted in the database.

Code:
        <bag name="LetterTexts" table="ComplianceCriteriaLetterText" cascade="none" inverse="false" lazy="false">
         <key column="CLT_CC_ID" />
         <one-to-many class="ComplianceLetterText"></one-to-many>
      </bag>



Change cascade="none" to cascade="all-delete-orphan" :)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: Update
PostPosted: Wed Oct 05, 2005 7:41 pm 
Newbie

Joined: Mon Jun 20, 2005 8:47 pm
Posts: 11
Hiya KPixel,

I tried that but with the same result.

Any other ideas?

Cheers,

Mike :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 3:02 am 
Regular
Regular

Joined: Fri Jun 11, 2004 6:27 am
Posts: 81
Location: Yaroslavl, Russia
Looks like you've incorrectly specified inverse="false" at your bag mapping. Try inverse="true". And cascade, of course, also must be all-delete-orphan.

_________________
Best,
Andrew Mayorov // BYTE-force


Top
 Profile  
 
 Post subject: Solution
PostPosted: Thu Oct 06, 2005 6:12 pm 
Newbie

Joined: Mon Jun 20, 2005 8:47 pm
Posts: 11
Thanks helpful people - that is the solution :D


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