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 #792157] Mon, 06 February 2012 12:22
Jan Ortmann is currently offline Jan Ortmann
Messages: 5
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?
Previous Topic:org.eclipse.persistence.exceptions.ValidationException
Next Topic:MOXy: JSON marshalling not using XMLType's propOrder
Goto Forum:
  


Current Time: Thu Jul 31 07:46:12 EDT 2014

Powered by FUDForum. Page generated in 0.01579 seconds