Removing non-optional bidirectional @OneToOne association with cascade

Description

Given a bidirectional @OneToOne relationship between Parent and Child Entity:

Parent.java

Child.java

The following test fails with org.hibernate.PropertyValueException: not-null property references a null or transient value: frol.Child.parent

See also the test case on GitHub.

I didn't find in JPA spec any restrictions on this kind of association.

Technically this kind of association can be persisted because the foreign key is on child side: first the persistence provider could delete the child and then its parent. But at runtime the deletion order seems to be different.

I found the following workarounds:

  • mark one end of the association as optional

  • make the association unidirectional

I use Hibernate 4.3.6.Final.

The problem also with Hibernate 4.1.5.Final but the exception is different. In this version Hibernate tries first to update the foreign key. Setting it to null causes ConsraintViolation because it is not nullable.

Attachments

1

Activity

Show:

Jan-Willem Gmelig Meyling July 20, 2018 at 11:11 AM

I am working on a test by the way, not a fix. I think its probably easiest to revert this commit and try another fix for HHH-9460.

Jan-Willem Gmelig Meyling July 20, 2018 at 11:10 AM

Created an issue here: https://hibernate.atlassian.net/browse/HHH-12842
Working on a PR, although the issue is quite obvious from following the path of execution (I've described my debug findings with pointers to the related code in the issue).

Guillaume Smet July 20, 2018 at 9:58 AM

could you open another issue and provide a test case?

Or even better a PR

Thanks!

Jan-Willem Gmelig Meyling July 20, 2018 at 9:49 AM

This fix seems to cause a regression that leads to non-optional OneToOne's to always be fetched eagerly, because isNullable() returns foreignKeyDirection == FROM_PARENT, which is not set any more :

Therefore,

  • foreignKeyDirection= ForeignKeyDirection.TO_PARENT

  • OneToOneType.isNullable returns true (used to be false because non optional/constrained)

  • A proxy will never be created

Roger Peyer December 6, 2017 at 1:09 PM

Any news on this issue?

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Created October 28, 2014 at 10:52 AM
Updated March 11, 2019 at 2:56 PM
Resolved February 21, 2018 at 12:56 PM