In both of the following cases, Hibernate creates a unique key constraint: 1) <many-to-one name="employee" column="employee_id" unique="true" not-null="true" /> 2) <many-to-one name="employee" column="employee_id" unique="true" not-null="false" />
The documentation is clear that setting unique="true" indicates that a unique key constraint should be created. I only see mappings for <many-to-one ... unique="true" not-null="true" > in the documentation. Is the combination in 2) valid (unique="true" not-null="false")? ----- Original Message ----- > From: "Gail Badner" <gbad...@redhat.com> > To: "hibernate-dev" <hibernate-dev@lists.jboss.org> > Sent: Tuesday, December 2, 2014 9:34:24 PM > Subject: [hibernate-dev] Mapping defaults for @OneToOne > > I'm a bit confused by the mapping defaults for @OneToOne defined in JPA 2.1. > > Sections 2.10.1 and 2.10.3.1 (Bidirectional and Unidirectional OneToOne > Relationships) says, "The foreign key column has the same type as the > primary key of table B and there is a unique key constraint on it." > > Section 11.1.41 (OneToOne Annotation) says that, by default, optional=true, > which translates into nullable foreign key column(s). > > IIUC, the default mapping is not supported by many (all?) databases, because > a constraint violation exception will be thrown if a column has a unique key > constraint and there is more than one null value for that column. > > Currently, Hibernate does not create a unique key unless @OneToOne( > optional=false ) is defined. I believe Hibernate is doing the right thing > here, but it is not consistent with the spec. > > Is the spec wrong in this case, or am I missing something? Please let me > know... > > Thanks, > Gail > _______________________________________________ > hibernate-dev mailing list > hibernate-dev@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/hibernate-dev > _______________________________________________ hibernate-dev mailing list hibernate-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/hibernate-dev