HHH-11539 - when foreignKey is added to an @JoinColumn, and not @CollectionTable directly, consider it as well

This commit is contained in:
Bogdan Stirbat 2017-07-30 19:54:44 +03:00 committed by Vlad Mihalcea
parent 742df1f3fa
commit 47f8a67990
2 changed files with 98 additions and 1 deletions

View File

@ -1134,6 +1134,13 @@ public abstract class CollectionBinder {
else {
key.setForeignKeyName( StringHelper.nullIfEmpty( collectionTableAnn.foreignKey().name() ) );
key.setForeignKeyDefinition( StringHelper.nullIfEmpty( collectionTableAnn.foreignKey().foreignKeyDefinition() ) );
if ( key.getForeignKeyName() == null &&
key.getForeignKeyDefinition() == null &&
collectionTableAnn.joinColumns().length == 1 ) {
JoinColumn joinColumn = collectionTableAnn.joinColumns()[0];
key.setForeignKeyName( StringHelper.nullIfEmpty( joinColumn.foreignKey().name() ) );
key.setForeignKeyDefinition( StringHelper.nullIfEmpty( joinColumn.foreignKey().foreignKeyDefinition() ) );
}
}
}
else {

View File

@ -27,6 +27,7 @@ import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@ -73,7 +74,9 @@ public class ForeignKeyConstraintTest extends BaseNonConfigCoreFunctionalTestCas
VehicleBuyInfo.class,
Car.class,
Truck.class,
Company.class
Company.class,
PlanItem.class,
Task.class
};
}
@ -132,6 +135,16 @@ public class ForeignKeyConstraintTest extends BaseNonConfigCoreFunctionalTestCas
assertForeignKey( "FK_VEHICLE_BUY_INFOS_VEHICLE", "VEHICLE_NR", "VEHICLE_VENDOR_NR" );
}
@Test
public void testMapForeignKeyJoinColumnColection() {
assertForeignKey( "FK_PROPERTIES_TASK", "task_id" );
}
@Test
public void testMapForeignKeyColection() {
assertForeignKey( "FK_ATTRIBUTES_TASK", "task_id" );
}
@Test
public void testAssociationOverride() {
// class level association overrides
@ -506,4 +519,81 @@ public class ForeignKeyConstraintTest extends BaseNonConfigCoreFunctionalTestCas
})
public CompanyInfo info;
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class PlanItem {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity
@SecondaryTable( name = "Task" )
public class Task extends PlanItem {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ElementCollection
@CollectionTable(
name = "task_properties",
joinColumns = {
@JoinColumn(
name = "task_id",
foreignKey = @ForeignKey(
name = "FK_PROPERTIES_TASK",
foreignKeyDefinition = "FOREIGN KEY (task_id) REFERENCES Task"
)
)
}
)
private Set<String> properties;
@ElementCollection
@CollectionTable(
name = "task_attributes",
joinColumns = {@JoinColumn(name = "task_id")},
foreignKey = @ForeignKey(
name = "FK_ATTRIBUTES_TASK",
foreignKeyDefinition = "FOREIGN KEY (task_id) REFERENCES Task"
)
)
private Set<String> attributes;
@Override
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
public Set<String> getProperties() {
return properties;
}
public void setProperties(Set<String> properties) {
this.properties = properties;
}
public Set<String> getAttributes() {
return attributes;
}
public void setAttributes(Set<String> attributes) {
this.attributes = attributes;
}
}
}