-->
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: Problem saving Transient Parent and Children at once
PostPosted: Thu Jan 17, 2008 12:14 pm 
Newbie

Joined: Thu Jan 17, 2008 11:27 am
Posts: 4
Hi there!

I´m having some problems with the persisting of transient objects with a collection of other transient child objects.
Maybe someone has seen this before and can open my eyes.

For the introduction, I´m using the Spring Framework and Hibernate 3.2 to persist objects with the following relationship. Really basic!

Code:
<class name="model.[b]Workstep[/b]" table="workstep">

   <id name="id" type="int" column="id" >
      <generator class="assigned" />
   </id>
    ...

    <set name="properties" inverse="true" cascade="save-update">
        <key column="workstep_id"/>
        <one-to-many class="model.WorkstepPropertyValue" />
    </set>
    ...
</class>


<class name="model.[b]WorkstepPropertyValue[/b]" table="property_values">

   <id name="id" type="int">
      <column name="id" />
      <generator class="assigned" />
   </id>
      
   <many-to-one name="property" class="model.[b]WorkstepProperty[/b]" column="property_id"/>
      
   <many-to-one name="workstep" class="model.Workstep" column="workstep_id" not-null="true"/>
      
   <property name="value" type="string"><column name="value"/></property>

</class>



I have a HTML Form, where the users define the Workstep data and the same time the new PropertyValues (the children). This Form is sent to the Controller and there I try to persist the new Workstep and its PropertyValues.

Hier the code to persist the objects. Pretty simple.

in the controller:
Code:
    // w is the new Workstep with the new children
    // command is sent from the form
    Workstep w = (Workstep)command;
    genericDao.save(w);


in genericDao:
Code:
    public void save(Object o) {
        getHibernateTemplate().saveOrUpdate(o);
    }



On calling the save method from the HibernateTemplate, I am getting the Exception:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [model.WorkstepPropertyValue#0].

I have already searched in this forum for a problem like this, but nobody has the same problem with transient parents and children.
On the Hibernate documentation it is also not really explained.

After that, I also tried not to cascade the save process and persist first the Parent object (Workstep) and then the children (PropertyValues) after in a service class, but I also didn´t work, because the last saved parent object had no ID at the time I wanted to persist its children. Also weird :(

Does anybody know, why this Exception and perhaps how to solve it???
I would be very very very thankful, because I´ve been trying to get it to work since a couple of days already.

Thanks in advance!

Wagner
Hamburg - Germany


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.