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