Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] The problem with child.Parent(this) in a property access setter?

Hello,

Because you have configured your entity to use Property access, the JPA provider is also required to use the set/get methods on the entity. The reason why is complex, but boils down to EclipseLink's internal processes also using the set method when reading in entities, dealing with incomplete objects and clones while it builds the object tree. The problem you are experiencing is likely not due to the remove, but that the EnttiyManager gets corrupted when B is read in from the database - B could be left referencing an unmanaged A that that it then thinks is new. Anyways, the solution is to use field access if you are going to set logic in the get/set methods.
Best Regards,
Chris

If you are going to have logic in your set method

Mike Traum wrote:
Hi,
I recently got bit by the scenario described here:
http://en.wikibooks.org/wiki/Java_Persistence/Mapping#Odd_behavior

I have A->B->C. Those entities are using property access. In the setters of the children, I am setting their parents, like this snippet from the class A:
 public void setChildren(List<B> children) {
   this.children = children;
   for (B b : children) {
     b.setParent(this);
   }
 }

When I try to remove a B (with the relationship severed), eclipselink tries to insert a duplicate A, throwing an exception.

This seems like a reasonable method, as the bidirectional relationship does not have to manually be maintained. Can anyone describe why this occurs so that I might avoid similar pitfalls in the future?

Thanks,
mike

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top