Note: I've moved this into a seperate thread as I think it's unrelated to
this one.
I'm having some trouble removing objects from a persistent set (one-to-many). When I attempt to remove a child using the same instance of the set the child object was added to an ObjectDeletedException expection is thrown with "deleted object would be re-saved by cascade (remove deleted object from associations)".
Here is a simplified version of what I'm attempting to do:
Code:
// add the child to the parent
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
// NOTE: p is then placed into the HttpSession
session.save(c); // at this point the c.childId is assigned a value
session.flush();
// re-attach the parent that was placed in the session
session.lock(p, LockMode.NONE);
// using the same child id returned in the save code
Child c = (Child) session.load(Child.class, cid);
// IMPORTANT: the remove call below returns false, however, using the debugger I can see the instance of Child is there
p.getChildren().remove(c); //
session.delete(c); // throws ObjectDeletedException: deleted object would be re-saved by cascade
session.flush();
I can however delete children that havn't been added in the current HttpSession...
The childId property is a Long, assigned using the seqhilo generator. Here are the equals and hashCode methods (although the equals method isn't called when removing the child):
Code:
public boolean equals(Object o) {
log.debug("equals called");
if (this == o) return true;
if (!(o instanceof Child)) return false;
final Child child = (Child) o;
if (childId != null ? !childId.equals(child.childId) : child.childId != null) return false;
return true;
}
public int hashCode() {
log.debug("hashCode called");
return (childId != null ? childId.hashCode() : 0);
}
I presume this doesn't work because when I added the Child to the parent the childId was null???
Anyone have any hints? What am I doing wrong?
Hibernate version: Hibernate 2.1.7
DatabasePostgreSQL 7.5
Mapping documents:one-to-many mapping in Parent:
Code:
<set name="children" lazy="true" inverse="true" cascade="all" sort="unsorted">
<key column="parent_id"> </key>
<one-to-many class="Child"/>
</set>
many-to-one mapping in Child:
Code:
<many-to-one name="parent" class="Parent" cascade="save-update" outer-join="auto" update="true" insert="true" access="property" column="parent_id" not-null="true" unique="false"/>