The behavior between Annotation based config and HBM-XML based config is different in case of One-To-One Assocations with optional=false. Given the following Parent->Child relation
class Parent {
@OneToOne(optional = false)
private Child child;
}
class Child {
@ManyToOne
@JoinColumn(name="PROPREFID")
private Parent parent;
}
When you try to save the parent with child=null you get, as expected, a PropertyValueException which states that Parent.child could not be null. In case of definig the same relationship in hbm.xml
<one-to-one cascade="all" constrained="true"
entity-name="org.hibernate.bugs.model.Child"
name="child" />
<many-to-one column="PROPREFID"
entity-name="org.hibernate.bugs.model.Parent"
name="parent" unique="true" not-null="true"/>
There is no PropertyValueException. There is a ConstraintViolationException from the database. This is a completly different behavior. In case of HBM-XML config the One-To-One relationship is mapped to org.hibernate.boot.model.source.internal.hbm.SingularAttributeSourceOneToOneImpl In this class the property isInsertable is always false. (Line 157). In case of Annotation based config the property isInsertable depends on property optional from the annotation. isInsertable is used when checkin if a value could be null. |