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: Best Practice For Deleting Member From Nested Collection
PostPosted: Wed Oct 28, 2009 8:33 am 
Newbie

Joined: Mon Sep 28, 2009 11:29 am
Posts: 4
Hi,

I'm trying to find the best practice for deleting a member from a collection that is nested within an aggregate. (DDD)

I have a Person class that contains an address collection. Inserts and Updates of addresses are successfully done through the Person class. I also want to delete an address through the Person class. If I just remove an address from the collection and call Session.Update(person) I get the following error:

Quote:
Cannot insert the value NULL into column 'PersonId', table 'PutTogether.dbo.PersonPhone'; column does not allow nulls. UPDATE fails.
The statement has been terminated.


The configuration for the Person on the Address is:
Code:
<many-to-one class="DomainModel.Person, DomainModel, Version=1.0.0.0, Culture=neutral,
          PublicKeyToken=null" foreign-key="FK_PersonAddress_Person" name="Person">
           <column name="PersonId" not-null="true" />
</many-to-one>


The configuration for the Addresses on the Person is:
Code:
<bag cascade="all" name="Addresses">
      <key>
            <column name="PersonId" />
       </key>
       <one-to-many class="DomainModel.PersonAddress, DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>


Thanks for your time,
Mark


Top
 Profile  
 
 Post subject: Re: Best Practice For Deleting Member From Nested Collection
PostPosted: Wed Oct 28, 2009 11:00 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try

<bag cascade="all-delete-orphan" inverse="true">

and don't forget to set adr.Person = null. Removing it from the collection isn't enough.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Best Practice For Deleting Member From Nested Collection
PostPosted: Wed Oct 28, 2009 2:10 pm 
Newbie

Joined: Mon Sep 28, 2009 11:29 am
Posts: 4
Thanks for the response.

Here is the code and changes made. I also tested it without setting the Person to null. It's working fine this way as well.

Code:
public void DeleteAddress(int id, Guid personId) {
     var customer = GetById(personId);
     var address = GetExistingAddress(customer, id);
     customer.Addresses.Remove(address);
     Session.Update(customer);   
}

private PersonAddress GetExistingAddress(Customer customer, int id) {
     return customer.Addresses.Where(x => x.Id == id).FirstOrDefault();
}


<bag cascade="all-delete-orphan" inverse="true" name="Addresses">
     <key>
          <column name="PersonId" />
     </key>
     <one-to-many class="DomainModel.PersonAddress, DomainModel, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null" />
  </bag>


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.