diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java index fc30ceaad7..87391077c0 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/TableBinder.java @@ -600,8 +600,8 @@ public class TableBinder { } while ( idColumns.hasNext() ) { Column column = (Column) idColumns.next(); - columns[0].overrideFromReferencedColumnIfNecessary( column ); columns[0].linkValueUsingDefaultColumnNaming( column, referencedEntity, value ); + columns[0].overrideFromReferencedColumnIfNecessary( column ); } } else { diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ImplicitCompositeKeyJoinTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ImplicitCompositeKeyJoinTest.java new file mode 100644 index 0000000000..4a0557cfa3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ImplicitCompositeKeyJoinTest.java @@ -0,0 +1,181 @@ +/* + * 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 . + */ +package org.hibernate.test.schemaupdate; + +import javax.persistence.Column; +import javax.persistence.Embeddable; +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.List; + +import org.apache.log4j.Logger; + +import org.hibernate.annotations.ForeignKey; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.tool.schema.internal.SchemaCreatorImpl; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; + +import static org.junit.Assert.assertTrue; + +/** + * @author Andrea Boriero + */ +@TestForIssue(jiraKey = "HHH-9865") +public class ImplicitCompositeKeyJoinTest { + private static final Logger LOGGER = Logger.getLogger( ImplicitCompositeKeyJoinTest.class ); + + private final static String EXPECTED_SQL = "create table Employee " + + "(age varchar(15) not null" + + ", birthday varchar(255) not null" + + ", name varchar(20) not null" + + ", manager_age varchar(15)" + + ", manager_birthday varchar(255)" + + ", manager_name varchar(20)" + + ", primary key (age, birthday, name))"; + + @Test + public void testImplicitCompositeJoin() throws Exception { + + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build(); + try { + org.hibernate.boot.Metadata metadata = new MetadataSources( ssr ) + .addAnnotatedClass( Employee.class ) + .buildMetadata(); + + boolean passed = false; + + List commands = new SchemaCreatorImpl().generateCreationCommands( + metadata, + false + ); + for ( String command : commands ) { + LOGGER.info( command ); + + if ( EXPECTED_SQL.equals( command ) ) { + passed = true; + } + } + assertTrue( + "Expected create table command for Employee entity not found", + passed + ); + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Entity + @Table(name = "Employee") + public class Employee { + @EmbeddedId + @ForeignKey(name = "none") + private EmployeeId id; + + @ManyToOne(optional = true) + @ForeignKey(name = "none") + private Employee manager; + + public void setId(EmployeeId id) { + this.id = id; + } + + public EmployeeId getId() { + return id; + } + + public void setManager(Employee manager) { + this.manager = manager; + } + + public Employee getManager() { + return manager; + } + } + + @Embeddable + public class EmployeeId implements Serializable { + private static final long serialVersionUID = 1L; + + public EmployeeId(String name, String birthday, String age) { + this.name = name; + this.birthday = birthday; + this.age = age; + } + + @Column(length = 15) + public String age; + + @Column(length = 20) + private String name; + + private String birthday; + + @Override + public int hashCode() { + int hash = 1; + hash = hash * 31 + (name != null ? name.hashCode() : 0); + hash = hash * 31 + (age != null ? age.hashCode() : 0); + return hash * 31 + (birthday != null ? birthday.hashCode() : 0); + } + + @Override + public boolean equals(Object obj) { + if ( obj == this ) { + return true; + } + + if ( !(obj instanceof EmployeeId) ) { + return false; + } + EmployeeId that = (EmployeeId) obj; + if ( age != that.age ) { + return false; + } + if ( birthday != that.birthday ) { + return false; + } + if ( name != null && !name.equals( that.name ) ) { + return false; + } + return true; + } + + public void setAge(String age) { + this.age = age; + } + + public void setName(String name) { + this.name = name; + } + + + public void setBirthday(String birthday) { + this.birthday = birthday; + } + + public String getAge() { + return age; + } + + public String getName() { + return name; + } + + public String getBirthday() { + return birthday; + } + } +}