I am basically not deleting the address first, because I don't want to 
replace the address instance. It is exactly the sort of thing I want to 
avoid. But to do that I have to make sure the setter is not public.
But what I was wondering was if there was an option in the NHibernate 
mapping mechanism that I overlooked that would prevent this behaviour, but 
this can only be done when you also have a reference from address to either 
Person or Organization.

The reason we didn't have separate objects like you suggest is that they 
are basically the same and (as for now) don't need a reference back to the 
owners. But since we will probably have to support multiple address per 
organization or persons in the future we will probably go for an extra 
table that keeps track of the references and the type of address it is.

David.

On Tuesday, 2 September 2014 05:59:04 UTC+2, MattO wrote:
>
> Why aren't you deleting the address first, thus no orphans occur?  I guess 
> I wasn't really aware you could cause orphans in this way, but I guess it 
> makes sense.  
>
> It seems to me that you need to write a function to see if the address is 
> still used by either the Person or the Organisation objects.  And if not 
> then Delete the address, otherwise keep the address.
>
> Another design may be to have OrganizationAddresses and PersonAddresses 
> objects that are different from each other.
>
> Matt
>
> On Monday, September 1, 2014 9:04:18 AM UTC-5, David Perfors wrote:
>
>> Hello,
>>
>> We have the following table structure:
>> | Address |
>> -----------
>> | Id      |
>> | Street  |
>> | City    |
>> -----------
>>
>> | Person              |
>> -----------------------
>> | Id                  |
>> | Name                |
>> | VisitingAddressId   |
>> | PostalAddressId     |
>> | <Some extra fields> |
>> -----------------------
>>
>> | Organisation        |
>> -----------------------
>> | Id                  |
>> | Name                |
>> | VisitingAddressId   |
>> | PostalAddressId     |
>> | <Some extra fields> |
>> -----------------------
>>
>> Basically our objects are modelled the same way. The Person and 
>> Organisation are completely different and should not inherit from each 
>> other in any way.
>> The mapping we use is basically the following (both Person and 
>> Organisation have a similar mapping):
>>
>> public class PersonMap : ClassMap<Person>
>> {
>>      public PersonMap()
>>      {
>>              Id(x => x.Id);
>>              Map(x => x.Name);
>>              References(x => x.VisitingAddress).Cascade.All();
>>              References(x => x.PostalAddress).Cascade.All();
>>      }
>> }
>>
>>
>> From address there is (at the moment) no mapping to either Person or 
>> Organisation.
>> Since the visiting has a public setter, the addresses could be overridden 
>> by replacing the address instance with a new instance of Address, when 
>> saving the Person or Organisation, the original address will be orphaned.
>> Is there a way (other than making the setters non public) to prevent this?
>>
>> Kind regards,
>> David Perfors
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to