|Bidirectional relationship with orphan removal: NULL-Update to a NOT NULL Column [message #792160]
||Mon, 06 February 2012 12:22
| Jan Ortmann
Registered: July 2009
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.
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
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?
Powered by FUDForum
. Page generated in 0.02203 seconds