Having a problem with a NonUniqueException

In my application I need to display, add & update Thing objects in various places, so I've created a ThingForm component that I can simply drop in and pass in the required parameters and it should work.

Page Description
A page displays some info about the ParentThing and also presents a form to add another Thing to its set of Things, once submitted the page displays again with the filled in information still there so the user can make ammendments to the Thing and submit again.


Domain Classes
ParentThing - An object that contains many Thing objects
ParentThingManager - A service object which handles the crud operations for ParentThings
Thing - An object that is associated to a ParentThing


ParentThing 1---M Thing (Cascade="save-update")

Tapestry Component
ThingFormComponent - A common form used in my application to add & edit Thing objects


Parameters
ThingManager - A Service Manager that handles the saving of the Thing object using Spring/HibernateDAOs
ParentThing - An object that is contains many Thing objects as a Set
[Thing] - An optional Thing object to pass into the component & therefore display field values



Page Flow In my page spec a property is defined to create a ParentThingManager

The id of the ParentThing is in my visit object, so in pageBeginRender I have code that retrieves my ParentThing from the DB (using ParentThingManager (Spring/Hibernate DAO)) and then calls setParentThing(pt) property on my page.

The template contains: <span jwcid="@ThingFormComponent" manager="ognl:thingManager" thing="ognl:thing">

My ThingFormComponent pageBeginRender() method creates an empty Thing object if necessary.

User fills out the form and submits

This then calls save() listener which does the following:
        // Set the associations on both sides for Hibernate
        getThing().setParentThing(getParentThing());
        getParentThing().getThings().add(getThing());

        getParentThingManager().saveParentThing(getParentThing());

        return;

The Thing is added to the ParentThing & saved to the DB correctly

The page redisplays with all the fields filled out, user resubmits but this throws a hibernate NonUniqueObjectException "a different object with the same identifier value was already associated with the session: 22, of class: com.company.Thing"

Now I get why this is happening (I think) - because on submission the ParentThing is retrieved therefore bring back "Thing:22" as part of its Set of Things so "Thing:22" now exits in the hib session, I then re-insert the Thing from the form which also has the id of 22 and hibernate realises that the Thing:22 in its session is different to the new one and throws a wobbly (exception).

So how do I get round this?, I'd like it to do a saveOrUpdateCopy() on its child Things when it does its cascade

Am I correct in my thinking and how do I get round this.

Will post to the hibernate forum as well.

Many thanks,

Adam


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to