diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java index 77be05530038..b62fd25f1317 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java @@ -253,7 +253,7 @@ private static Ejb3JoinColumn buildJoinColumn( String suffixForDefaultColumnName, MetadataBuildingContext buildingContext) { if ( ann != null ) { - if ( BinderHelper.isEmptyAnnotationValue( mappedBy ) ) { + if ( ! BinderHelper.isEmptyOrNullAnnotationValue( mappedBy ) ) { throw new AnnotationException( "Illegal attempt to define a @JoinColumn with a mappedBy association: " + BinderHelper.getRelativePath( propertyHolder, propertyName ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OverrideOneToOneJoinColumnTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OverrideOneToOneJoinColumnTest.java new file mode 100644 index 000000000000..09de4a45f509 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OverrideOneToOneJoinColumnTest.java @@ -0,0 +1,56 @@ +package org.hibernate.test.annotations.onetoone; + +import org.hibernate.AnnotationException; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.model.naming.Identifier; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.mapping.ForeignKey; +import org.hibernate.mapping.Table; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Aresnii Skvortsov + */ +@TestForIssue(jiraKey = "HHH-4384") +public class OverrideOneToOneJoinColumnTest extends BaseUnitTestCase { + @Test + public void allowIfJoinColumnIsAbsent() { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); + try { + Metadata metadata = new MetadataSources(ssr) + .addInputStream(getClass().getResourceAsStream("override-absent-join-column.orm.xml")) + .buildMetadata(); + + Table childTable = metadata.getDatabase().getDefaultNamespace().locateTable(Identifier.toIdentifier("Son")); + ForeignKey fatherFk = (ForeignKey) childTable.getForeignKeyIterator().next(); + assertEquals("Overridden join column name should be applied", fatherFk.getColumn(0).getName(), "id_father"); + } finally { + StandardServiceRegistryBuilder.destroy(ssr); + } + } + + @Test + public void disallowOnSideWithMappedBy() { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); + try { + new MetadataSources(ssr) + .addInputStream(getClass().getResourceAsStream("join-column-on-mapped-by.orm.xml")) + .buildMetadata(); + fail("Should disallow @JoinColumn override on side with mappedBy"); + } catch (AnnotationException ex) { + assertTrue("Should disallow exactly because of @JoinColumn override on side with mappedBy", + ex + .getMessage() + .startsWith("Illegal attempt to define a @JoinColumn with a mappedBy association:") + ); + } finally { + StandardServiceRegistryBuilder.destroy(ssr); + } + } +} diff --git a/hibernate-core/src/test/resources/org/hibernate/test/annotations/onetoone/join-column-on-mapped-by.orm.xml b/hibernate-core/src/test/resources/org/hibernate/test/annotations/onetoone/join-column-on-mapped-by.orm.xml new file mode 100644 index 000000000000..350f375c9b28 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/annotations/onetoone/join-column-on-mapped-by.orm.xml @@ -0,0 +1,18 @@ + + + + + org.hibernate.test.annotations.onetoone + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/resources/org/hibernate/test/annotations/onetoone/override-absent-join-column.orm.xml b/hibernate-core/src/test/resources/org/hibernate/test/annotations/onetoone/override-absent-join-column.orm.xml new file mode 100644 index 000000000000..5e069b149c9d --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/test/annotations/onetoone/override-absent-join-column.orm.xml @@ -0,0 +1,32 @@ + + + + + org.hibernate.test.annotations.onetoone + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file