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.  [ 1 post ] 
Author Message
 Post subject: State Pattern - Replacing state objects causing issues.
PostPosted: Fri Sep 08, 2006 3:17 am 
Beginner
Beginner

Joined: Fri Sep 08, 2006 2:55 am
Posts: 21
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.