Nullable check should not be skipped for OneToOne annotated with @NotFound(action = NotFoundAction.IGNORE)

Activity

Show:

Artem K.March 23, 2017 at 5:38 PM

It has to be nullable=false though, the two columns constitute the primary key of the join table.

My research (with limited knowledge of Hibernate Code) shows the problem in org.hibernate.mapping.Property initialization for the association @...ToOne. Nullability check calls the following method of this class:

Implementation of isNullable() simply iterates over all join columns and checks that at least one nullable=true. When we do not use @JoinTable, this condition will be true. When we use @JoinTable the condition is false because all join columns are non-nullable.

So for this method to return true independently of column nullability, Hibernate should set "optional" field of "Property" instance. This never happens, optional field is always false for any @...ToOne annotation, whether it's declared as optional or not. In my option, the fix is to set this field to the value of the optional attribute of the annotation.

Another thing to keep in mind is invalid mapping on programmer's part. If somebody declares @OneToOne(optional=true) and @JoinColumn(nullable=false), they contradict each other. Based on the code I saw, the current implementation will consider the association not optional. My proposed solution will make such a mapping optional but physically impossible because column is not nullable. I do not know what the middle ground should be.

Andrea BorieroMarch 23, 2017 at 5:25 PM

,

thanks for the tests.
I'm going to open 2 other jiras one for the OneToOne case and another for the ManyToOne.
What causes the ManyToOne failure seems to be the nullable = false in the JoinColumn of the inverseJoinColumns.

Artem K.March 23, 2017 at 3:47 PM

I see that you created a new ticket for the discussion we started in HHH-11305. Here is a modified testcase from that issue that shows the same problem with @ManyToOne.

Also, can this be backported to 5.1 please? Latest stable Wildfly comes with 5.1 due to various migration complexities so I think it's important to have this fixed in 5.1 tree. Thanks.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created March 23, 2017 at 2:12 PM
Updated April 14, 2017 at 10:13 AM
Resolved March 28, 2017 at 10:42 AM