Bidirectional relationship with @NotNull on both sides fails to save

Description

Simular to
https://hibernate.atlassian.net/browse/HHH-14129
we have a bidirectional relationshipt between parent and child. But we have both sides marked as '@NotNull'.
This works fine until release 5.4.19. Starting with relesase 5.4.20 the save methode fails with the following Exception:

org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : org.hibernate.test.annotations.manytoone.NotNullManyToOneTest$Parent.child -> org.hibernate.test.annotations.manytoone.NotNullManyToOneTest$Child

To reproduce the exception you can use the NotNullManyToOneTest.

Just add the @NotNull annotation to the child property of the parent entity.

Activity

Show:

Andrea Boriero March 9, 2021 at 11:45 AM

Hi ,

you are welcome!

I want to investigate a little more before closing this Jira.

Oliver Breidenbach March 8, 2021 at 10:53 AM

Hi Andrea,

thank you for your help. It helped me a lot. I have a solution for my problem. So feel free to close this ticket. But I still think that hibernate is to strikt hear. The not null constraints could be deffered.

Thanks agaein

Oliver

Oliver Breidenbach March 7, 2021 at 12:44 PM

With hibernate.validator.apply_to_ddl=false the failure is gone. So yes this could be a solution.

I still does not understand why on a bidirectional connection not both sides can be non null. The constrainsts could be deferred. This should perfectly work.

Andrea Boriero March 5, 2021 at 4:30 PM

in the case of NotNullManyToOneTest the mapping produces 2 tables having both a not null Foreign Key and this is the source of the problem.

in case of ValidationMode.CALLBACK the applyRelationalConstraints is not executed and then property.setOptional( !hasNotNull ); is not applied (see TypeSafeActivator line 146) but javax.persistence.validation.mode is considered only when EntityManager is used.

In your case probably a solution is to set hibernate.validator.apply_to_ddl=false

Oliver Breidenbach March 5, 2021 at 2:05 PM

Hi Andrea,

I beleave the following is happing.

On the parent.child property I have the @NotNull annotation but not optional=false set.

On the child.parent property I have the @NotNull annotation and optional=false set.

This works fine until 5.4.19. With release 5.4.20 you added property.setOptional( !hasNotNull ); to TypeSafeActivator. Now both side of the relation are optional=false, and hibernate fails to save the parent entity.

https://github.com/hibernate/hibernate-orm/blob/17c5fab50e9a1c90a96a01498b8c95b1646194cc/hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivator.java#L353

My testcase did not use bean validation and so the optional was not changed to optional=false. If I set optional=false I get the same failure as in my application.
I do not understand what is the problem with a bidirectional reference with optional=false on both sides.

Details

Assignee

Reporter

Worked in

Components

Affects versions

Priority

Created March 1, 2021 at 11:55 AM
Updated March 9, 2021 at 11:45 AM