HHH-8805 - [SchemaUpdate] javax.persistence.ForeignKey doesn't respect ConstraintMode.NO_CONSTRAINT

This commit is contained in:
Robert Rettig 2018-07-09 19:54:01 +02:00 committed by Vlad Mihalcea
parent d4477ff5f8
commit 831bff13be
4 changed files with 322 additions and 4 deletions

View File

@ -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;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}