Skip to content

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

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

Merged
merged 1 commit into from
May 5, 2022
Merged
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,204 @@
package org.hibernate.test.annotations.onetoone;

import javax.persistence.AssociationOverride;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MappedSuperclass;
import javax.persistence.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.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* @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 )
.addAnnotatedClass( Person.class )
.addAnnotatedClass( State.class )
.buildMetadata();

Table personTable = metadata.getDatabase().getDefaultNamespace().locateTable( Identifier.toIdentifier(
"PERSON_TABLE" ) );
ForeignKey foreignKey = personTable.getForeignKeyIterator().next();

assertEquals(
"Overridden join column name should be applied",
"PERSON_ADDRESS_STATE",
foreignKey.getColumn( 0 ).getName()
);

}
finally {
StandardServiceRegistryBuilder.destroy( ssr );
}
}

@Test
public void disallowOnSideWithMappedBy() {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
try {
new MetadataSources( ssr )
.addAnnotatedClass( Employee.class )
.addAnnotatedClass( PartTimeEmployee.class )
.addAnnotatedClass( Desk.class )
.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 );
}
}

@Entity(name = "Person")
@javax.persistence.Table(name = "PERSON_TABLE")
public static class Person {

private String id;

private Address address;

@Id
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Embedded
@AssociationOverride(name = "state", joinColumns = { @JoinColumn(name = "PERSON_ADDRESS_STATE") })
public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}
}

@Embeddable
public static class Address {

private String street;

private String city;

private State state;

@OneToOne
public State getState() {
return state;
}

public void setState(State state) {
this.state = state;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}
}

@Entity(name = "State")
@javax.persistence.Table(name = "STATE_TABLE")
public static class State {

private String id;

private String name;

@Id
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

@MappedSuperclass
public static class Employee {

@Id
private Long id;

private String name;

@OneToOne(mappedBy = "employee")
protected Desk desk;
}

@Entity
@AssociationOverride(name = "desk",
joinColumns = @JoinColumn(name = "PARTTIMEEMPLOYEE_DESK"))
public static class PartTimeEmployee extends Employee {

}

@Entity(name = "Desk")
public static class Desk {
@Id
private Long id;

@OneToOne
private PartTimeEmployee employee;

private String location;
}


}