Skip to content

HHH-4384 Allow join column override if @JoinColumn is absent on @OneToOne(mappedBy = ""). #3057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
version="2.0">
<package>org.hibernate.test.annotations.onetoone</package>
<entity class="Party"/>
<entity class="PartyAffiliate">
<association-override name="party">
<join-column name="id_party"/>
</association-override>
</entity>
</entity-mappings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
version="2.0">
<package>org.hibernate.test.annotations.onetoone</package>
<entity class="Father">
<attributes>
<id name="id">
<generated-value strategy="AUTO"/>
</id>
<basic name="name"/>
</attributes>
</entity>
<entity class="Son">
<association-override name="father">
<join-column name="id_father"/>
</association-override>
<attributes>
<id name="id">
<generated-value strategy="AUTO"/>
</id>
<basic name="name"/>
<one-to-one name="father"/>
</attributes>
</entity>
</entity-mappings>