| 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:
public boolean isOptional() {
return optional || isNullable();
}
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. |