diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java index 19ede93efe..ad96518092 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java @@ -1006,12 +1006,11 @@ public class EntityBinder { SecondaryTables secondaryTables = annotatedClass.getAnnotation( SecondaryTables.class ); if ( secondaryTables != null ) { - for ( SecondaryTable secondaryTable2 : secondaryTables.value() ) { - if ( secondaryTable != null && nameToMatch.equals( secondaryTable.name() ) ) { - return secondaryTable; + for ( SecondaryTable secondaryTablesEntry : secondaryTables.value() ) { + if ( secondaryTablesEntry != null && nameToMatch.equals( secondaryTablesEntry.name() ) ) { + return secondaryTablesEntry; } } - } return null; diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintSecondaryTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintSecondaryTableTest.java new file mode 100644 index 0000000000..fa4b0af2e1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintSecondaryTableTest.java @@ -0,0 +1,106 @@ +/* + * 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 java.io.File; +import java.nio.file.Files; +import java.util.EnumSet; +import javax.persistence.ConstraintMode; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.SecondaryTable; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.hibernate.tool.schema.TargetType; + +import org.hibernate.testing.RequiresDialect; +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.assertFalse; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(H2Dialect.class) +@TestForIssue(jiraKey = "HHH-8805") +public class SchemaUpdateJoinColumnNoConstraintSecondaryTableTest extends BaseUnitTestCase { + + private static final String EXPECTED_SCRIPT = + " create table Child ( " + + " id bigint not null, " + + " some_fk bigint, " + + " primary key (id) " + + " ); " + + " " + + " create table Parent ( " + + " id bigint not null, " + + " primary key (id) " + + " ); "; + private static final String DELIMITER = ";"; + + @Test + public void test() throws Exception { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( Environment.HBM2DDL_AUTO, "none" ) + .build(); + try { + File output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addAnnotatedClass( Parent.class ) + .buildMetadata(); + metadata.validate(); + + new SchemaUpdate() + .setHaltOnError( true ) + .setOutputFile( output.getAbsolutePath() ) + .setDelimiter( DELIMITER ) + .setFormat( true ) + .execute( EnumSet.of( TargetType.SCRIPT ), metadata ); + + String outputContent = new String(Files.readAllBytes(output.toPath())); + + assertFalse( outputContent.toLowerCase().contains( "foreign key" ) ); + + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Entity(name = "Parent") + @SecondaryTable( + name = "ParentDetails", + foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT) + ) + public static class Parent { + + @Id + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintSecondaryTablesTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintSecondaryTablesTest.java new file mode 100644 index 0000000000..f1e426c5d2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintSecondaryTablesTest.java @@ -0,0 +1,106 @@ +/* + * 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 java.io.File; +import java.nio.file.Files; +import java.util.EnumSet; +import javax.persistence.ConstraintMode; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.SecondaryTable; +import javax.persistence.SecondaryTables; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.hibernate.tool.schema.TargetType; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(H2Dialect.class) +@TestForIssue(jiraKey = "HHH-8805") +public class SchemaUpdateJoinColumnNoConstraintSecondaryTablesTest extends BaseUnitTestCase { + + private static final String EXPECTED_SCRIPT = + " create table Child ( " + + " id bigint not null, " + + " some_fk bigint, " + + " primary key (id) " + + " ); " + + " " + + " create table Parent ( " + + " id bigint not null, " + + " primary key (id) " + + " ); "; + private static final String DELIMITER = ";"; + + @Test + public void test() throws Exception { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( Environment.HBM2DDL_AUTO, "none" ) + .build(); + try { + File output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addAnnotatedClass( Parent.class ) + .buildMetadata(); + metadata.validate(); + + new SchemaUpdate() + .setHaltOnError( true ) + .setOutputFile( output.getAbsolutePath() ) + .setDelimiter( DELIMITER ) + .setFormat( true ) + .execute( EnumSet.of( TargetType.SCRIPT ), metadata ); + + String outputContent = new String(Files.readAllBytes(output.toPath())); + + assertFalse( outputContent.toLowerCase().contains( "foreign key" ) ); + + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Entity(name = "Parent") + @SecondaryTables( + @SecondaryTable( + name = "ParentDetails", + foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT) + ) + ) + public static class Parent { + + @Id + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintTest.java new file mode 100644 index 0000000000..1cc8f0be3f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaUpdateJoinColumnNoConstraintTest.java @@ -0,0 +1,107 @@ +/* + * 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 java.io.File; +import java.nio.file.Files; +import java.util.EnumSet; +import javax.persistence.ConstraintMode; +import javax.persistence.Entity; +import javax.persistence.ForeignKey; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.tool.hbm2ddl.SchemaUpdate; +import org.hibernate.tool.schema.TargetType; + +import org.hibernate.testing.RequiresDialect; +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.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(H2Dialect.class) +@TestForIssue(jiraKey = "HHH-8805") +public class SchemaUpdateJoinColumnNoConstraintTest extends BaseUnitTestCase { + + private static final String DELIMITER = ";"; + + @Test + public void test() throws Exception { + StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() + .applySetting( Environment.HBM2DDL_AUTO, "none" ) + .build(); + try { + File output = File.createTempFile( "update_script", ".sql" ); + output.deleteOnExit(); + + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addAnnotatedClass( Parent.class ) + .addAnnotatedClass( Child.class ) + .buildMetadata(); + metadata.validate(); + + new SchemaUpdate() + .setHaltOnError( true ) + .setOutputFile( output.getAbsolutePath() ) + .setDelimiter( DELIMITER ) + .setFormat( true ) + .execute( EnumSet.of( TargetType.SCRIPT ), metadata ); + + String outputContent = new String(Files.readAllBytes(output.toPath())); + + assertFalse( outputContent.toLowerCase().contains( "foreign key" ) ); + + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Entity(name = "Parent") + public static class Parent { + + @Id + private Long id; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } + + @Entity(name = "Child") + public static class Child { + + @Id + private Long id; + + @ManyToOne + @JoinColumn( + name = "some_fk", + foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT) + ) + private Parent parent; + } + +}