Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column
Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column [message #792160] Mon, 06 February 2012 17:22 Go to next message
Jan Ortmann is currently offline Jan OrtmannFriend
Messages: 6
Registered: July 2009
Junior Member
Szenario
Given a Parent class with a bidirectional 1-many relationship to a Child class. The collection holding the children is annotated with orphan removal. The reference to the parent in the child class with nullable=false.

Steps
1.) Load a parent with at least one child from the database with EntityManager.find(...).
2.) Remove the child from the collection in the parent.
3.) Set the parent-reference in the child to null.
4.) Commit the transaction

Result
EclipseLink updates the child row (before deleting it) and tries to set the foreign key column to the parent to NULL. This leads to a SQLIntegrityConstraintViolationException: Column 'PARENT_ID' cannot accept a NULL value.

Why set the reference to the parent to null?
The child object remains in memory and although the parent has owned it, it might still be accessed in the code. Keeping a reference to the parent in the child is an inconsistent state in the domain model.

Of course we can avoid the exception by allowing null in the foreign key column, but we lose the constraint in the database. In any case, it just seems one SQL statement too much.

I checked the JPA spec. It specifies only that you can't reassign child objects to another parent. It does not specify the NULL case.

So, is this a bug or wanted behaviour?
Re: Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column [message #793942 is a reply to message #792160] Wed, 08 February 2012 19:10 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

EclipseLink will process update before deletes (this is sometimes required to resolve foreign key constraints).

If you do not want the foreign key updated to null, then do not set the parent to null.


James : Wiki : Book : Blog : Twitter
Re: Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column [message #797226 is a reply to message #793942] Mon, 13 February 2012 07:45 Go to previous messageGo to next message
Jan Ortmann is currently offline Jan OrtmannFriend
Messages: 6
Registered: July 2009
Junior Member
Hi James,

not "nulling" the reference from the removed child to the parent leaves the domain model in an inconsistent state. So not setting it to null to get the persistence to work, seems more like a workaround than the proper solution.

By the way: Hibernate does not issue an update before the delete statement in this use case. I think it would be good, if the JPA spec would also specify the null case for @orphanRemoval. If I find the time, I'll suggest it.

Thanks for your reply & kind regards
Jan
Re: Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column [message #804360 is a reply to message #797226] Wed, 22 February 2012 14:34 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Setting the parent to null in general can be very dangerous. It is common for a child to share its parent's id, so if you null out the parent, you now have an object with a null id, which is very difficult to process.


James : Wiki : Book : Blog : Twitter
Re: Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column [message #806701 is a reply to message #804360] Sat, 25 February 2012 10:22 Go to previous message
Jan Ortmann is currently offline Jan OrtmannFriend
Messages: 6
Registered: July 2009
Junior Member
Ok, thats true, but we don't use compound keys.
Previous Topic:Internal Exception: Can not set Clob to String
Next Topic:No persistence provider
Goto Forum:
  


Current Time: Thu Apr 25 21:59:34 GMT 2024

Powered by FUDForum. Page generated in 0.03269 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top