Hi,
I've got a fairly simple question but I first thought I should explain what I'm trying to do.
Essentially I have a Customer class that has multiple states, I've used the state pattern so each of these states is represented by a class derived from CustomerState.
Obviously since i'm using the state pattern the state object of the customer can be replaced at run time. I thought this approach would be OK and so my mapping files are associated in this way:
customer mapping file:
<one-to-one name="_state" cascade="all-delete-orphan" access="_field" />
customer state mapping file:
<class name="SevenIM.Crm.Domain.Customer+CustomerState, SevenIM.Crm.Domain" dynamic-update="" table="Customer" lazy="false" discriminator-value="StatusId" > <id name="_CustomerId" column="ID" access="field"> <generator class="foreign"> <param name="property">_Customer</param> </generator> </id> <discriminator column="StatusId" /> <one-to-one name="_Customer" constrained="true" access="field"/> <subclass name="SevenIM.Crm.Domain.Customer+NewProspectState, SevenIM.Crm.Domain" discriminator-value="0" lazy="false" /> <subclass name="SevenIM.Crm.Domain.Customer+DeclinedState, SevenIM.Crm.Domain" discriminator-value="1" lazy="false" />
The problem is, and I'm sure this seems obvious to a lot of you, that when I replace the state and then save and flush the client I get an exception because i've now got 2+ ClientState objects associated with the session:
A first chance exception of type 'NHibernate.NonUniqueObjectException' occurred in NHibernate.dll
Additional information: a different object with the same identifier value was already associated with the session
I realise that one solution is to delete the old state before assigning the new one, however without having a major redesign that isn't possible. I was thus hoping that by having that having the cascade attribute specified on the one-to-one mapping from the client would allow it to automatically dump the new state when its no longer referenced.
Anyway I was therefore wondering if there is any way to get the behavior I want just using the NHibernate mapping files?
Thanks,
Colin Jack
|