Affects Version/s: None
Fix Version/s: None
The specification of the orphan removal feature (chapter 2.9) should explicitly specify if the parent reference can be set to null while the foreign key column is defined as NOT NULL.
Given two classes Parent and Child, each persisted in its own database table (PARENT and CHILD). The CHILD table contains a foreign key column PARENT_ID. This column should be NOT NULL, we don't allow orphans in the database . The Java classes implement a bidirectional relationship. The Parent class holds a collection of children and the Child class holds a reference to its parent. See the following two code snippets.
The specification should specify the above mentioned NULL-Handling.
What happens if I load a parent object with a child object from the database, remove the child from the parent and set the reference from the child to the parent to null (as it does not belong to the parent anymore).
In my opinion this should be allowed even if the foreign key column is NOT NULL, as the child does not belong to the parent anymore. The child object gets removed from the database through the orphan removal feature, the NOT NULL constraint is not violated. Keeping the child-to-parent reference in the object model would leave it in a corrupt state.
In Hibernate the above code snippet works. Eclipselink however first issues an update statement setting the PARENT_ID to null, violating the NOT NULL constraint. So the above code triggers an exception during flush(). The specification should specify the expected behavior.