Consider 2 'parent' and 'child' entities:
class Parent {
@OneToMany(cascade = CascadeType.ALL)
Set<Child> children;
}
class Child {
@ManyToOne(cascade = CascadeType.ALL)
Parent parent;
}
There is a bidirectional relation with cascade=all on both sides. In some cases, on attempt to do a merge operation on a mixed (attached/transient) graph of these kind of entities, two managed entities are being created for single transient 'parent' entity.
The easiest way to see what's going on is to examine attached test. Set breakpoint to bug.CascadeAttachedDetachedTest.java:70 and run this test. When breakpoint fires, examine contents of the 'managedVehicle' object (see attached managedVehicle_bad_transport.png): ![](https://hibernate.atlassian.net/secure/thumbnail/25632/_thumb_25632.png)
Here we see two attached entities created for transient $2to3 entity. The first one (managedVehicle.transports[0]) is valid. The second one (managedVehicle.transports[0].pickupNode.pickupTransports[0]) is invalid (all fields, including ID is null). Next step (s.flush() ) will result in 'not-null property name is null' error for that entity.
|