Skip to content

Commit 831bff1

Browse files
Robert Rettigvladmihalcea
authored andcommitted
HHH-8805 - [SchemaUpdate] javax.persistence.ForeignKey doesn't respect ConstraintMode.NO_CONSTRAINT
1 parent d4477ff commit 831bff1

File tree

4 files changed

+322
-4
lines changed

4 files changed

+322
-4
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/annotations/EntityBinder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,11 @@ private SecondaryTable findMatchingSecondaryTable(Join join) {
10061006

10071007
SecondaryTables secondaryTables = annotatedClass.getAnnotation( SecondaryTables.class );
10081008
if ( secondaryTables != null ) {
1009-
for ( SecondaryTable secondaryTable2 : secondaryTables.value() ) {
1010-
if ( secondaryTable != null && nameToMatch.equals( secondaryTable.name() ) ) {
1011-
return secondaryTable;
1009+
for ( SecondaryTable secondaryTablesEntry : secondaryTables.value() ) {
1010+
if ( secondaryTablesEntry != null && nameToMatch.equals( secondaryTablesEntry.name() ) ) {
1011+
return secondaryTablesEntry;
10121012
}
10131013
}
1014-
10151014
}
10161015

10171016
return null;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate;
8+
9+
import java.io.File;
10+
import java.nio.file.Files;
11+
import java.util.EnumSet;
12+
import javax.persistence.ConstraintMode;
13+
import javax.persistence.Entity;
14+
import javax.persistence.ForeignKey;
15+
import javax.persistence.Id;
16+
import javax.persistence.JoinColumn;
17+
import javax.persistence.ManyToOne;
18+
import javax.persistence.SecondaryTable;
19+
20+
import org.hibernate.boot.MetadataSources;
21+
import org.hibernate.boot.registry.StandardServiceRegistry;
22+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
23+
import org.hibernate.boot.spi.MetadataImplementor;
24+
import org.hibernate.cfg.Environment;
25+
import org.hibernate.dialect.H2Dialect;
26+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
27+
import org.hibernate.tool.schema.TargetType;
28+
29+
import org.hibernate.testing.RequiresDialect;
30+
import org.hibernate.testing.TestForIssue;
31+
import org.hibernate.testing.junit4.BaseUnitTestCase;
32+
import org.junit.Test;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
37+
/**
38+
* @author Vlad Mihalcea
39+
*/
40+
@RequiresDialect(H2Dialect.class)
41+
@TestForIssue(jiraKey = "HHH-8805")
42+
public class SchemaUpdateJoinColumnNoConstraintSecondaryTableTest extends BaseUnitTestCase {
43+
44+
private static final String EXPECTED_SCRIPT =
45+
" create table Child ( " +
46+
" id bigint not null, " +
47+
" some_fk bigint, " +
48+
" primary key (id) " +
49+
" ); " +
50+
" " +
51+
" create table Parent ( " +
52+
" id bigint not null, " +
53+
" primary key (id) " +
54+
" ); ";
55+
private static final String DELIMITER = ";";
56+
57+
@Test
58+
public void test() throws Exception {
59+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
60+
.applySetting( Environment.HBM2DDL_AUTO, "none" )
61+
.build();
62+
try {
63+
File output = File.createTempFile( "update_script", ".sql" );
64+
output.deleteOnExit();
65+
66+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
67+
.addAnnotatedClass( Parent.class )
68+
.buildMetadata();
69+
metadata.validate();
70+
71+
new SchemaUpdate()
72+
.setHaltOnError( true )
73+
.setOutputFile( output.getAbsolutePath() )
74+
.setDelimiter( DELIMITER )
75+
.setFormat( true )
76+
.execute( EnumSet.of( TargetType.SCRIPT ), metadata );
77+
78+
String outputContent = new String(Files.readAllBytes(output.toPath()));
79+
80+
assertFalse( outputContent.toLowerCase().contains( "foreign key" ) );
81+
82+
}
83+
finally {
84+
StandardServiceRegistryBuilder.destroy( ssr );
85+
}
86+
}
87+
88+
@Entity(name = "Parent")
89+
@SecondaryTable(
90+
name = "ParentDetails",
91+
foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
92+
)
93+
public static class Parent {
94+
95+
@Id
96+
private Long id;
97+
98+
public Long getId() {
99+
return id;
100+
}
101+
102+
public void setId(Long id) {
103+
this.id = id;
104+
}
105+
}
106+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate;
8+
9+
import java.io.File;
10+
import java.nio.file.Files;
11+
import java.util.EnumSet;
12+
import javax.persistence.ConstraintMode;
13+
import javax.persistence.Entity;
14+
import javax.persistence.ForeignKey;
15+
import javax.persistence.Id;
16+
import javax.persistence.SecondaryTable;
17+
import javax.persistence.SecondaryTables;
18+
19+
import org.hibernate.boot.MetadataSources;
20+
import org.hibernate.boot.registry.StandardServiceRegistry;
21+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
22+
import org.hibernate.boot.spi.MetadataImplementor;
23+
import org.hibernate.cfg.Environment;
24+
import org.hibernate.dialect.H2Dialect;
25+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
26+
import org.hibernate.tool.schema.TargetType;
27+
28+
import org.hibernate.testing.RequiresDialect;
29+
import org.hibernate.testing.TestForIssue;
30+
import org.hibernate.testing.junit4.BaseUnitTestCase;
31+
import org.junit.Test;
32+
33+
import static org.junit.Assert.assertFalse;
34+
35+
/**
36+
* @author Vlad Mihalcea
37+
*/
38+
@RequiresDialect(H2Dialect.class)
39+
@TestForIssue(jiraKey = "HHH-8805")
40+
public class SchemaUpdateJoinColumnNoConstraintSecondaryTablesTest extends BaseUnitTestCase {
41+
42+
private static final String EXPECTED_SCRIPT =
43+
" create table Child ( " +
44+
" id bigint not null, " +
45+
" some_fk bigint, " +
46+
" primary key (id) " +
47+
" ); " +
48+
" " +
49+
" create table Parent ( " +
50+
" id bigint not null, " +
51+
" primary key (id) " +
52+
" ); ";
53+
private static final String DELIMITER = ";";
54+
55+
@Test
56+
public void test() throws Exception {
57+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
58+
.applySetting( Environment.HBM2DDL_AUTO, "none" )
59+
.build();
60+
try {
61+
File output = File.createTempFile( "update_script", ".sql" );
62+
output.deleteOnExit();
63+
64+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
65+
.addAnnotatedClass( Parent.class )
66+
.buildMetadata();
67+
metadata.validate();
68+
69+
new SchemaUpdate()
70+
.setHaltOnError( true )
71+
.setOutputFile( output.getAbsolutePath() )
72+
.setDelimiter( DELIMITER )
73+
.setFormat( true )
74+
.execute( EnumSet.of( TargetType.SCRIPT ), metadata );
75+
76+
String outputContent = new String(Files.readAllBytes(output.toPath()));
77+
78+
assertFalse( outputContent.toLowerCase().contains( "foreign key" ) );
79+
80+
}
81+
finally {
82+
StandardServiceRegistryBuilder.destroy( ssr );
83+
}
84+
}
85+
86+
@Entity(name = "Parent")
87+
@SecondaryTables(
88+
@SecondaryTable(
89+
name = "ParentDetails",
90+
foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
91+
)
92+
)
93+
public static class Parent {
94+
95+
@Id
96+
private Long id;
97+
98+
public Long getId() {
99+
return id;
100+
}
101+
102+
public void setId(Long id) {
103+
this.id = id;
104+
}
105+
}
106+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.schemaupdate;
8+
9+
import java.io.File;
10+
import java.nio.file.Files;
11+
import java.util.EnumSet;
12+
import javax.persistence.ConstraintMode;
13+
import javax.persistence.Entity;
14+
import javax.persistence.ForeignKey;
15+
import javax.persistence.Id;
16+
import javax.persistence.JoinColumn;
17+
import javax.persistence.ManyToOne;
18+
19+
import org.hibernate.boot.MetadataSources;
20+
import org.hibernate.boot.registry.StandardServiceRegistry;
21+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
22+
import org.hibernate.boot.spi.MetadataImplementor;
23+
import org.hibernate.cfg.Environment;
24+
import org.hibernate.dialect.H2Dialect;
25+
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
26+
import org.hibernate.tool.schema.TargetType;
27+
28+
import org.hibernate.testing.RequiresDialect;
29+
import org.hibernate.testing.TestForIssue;
30+
import org.hibernate.testing.junit4.BaseUnitTestCase;
31+
import org.junit.Test;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertFalse;
35+
import static org.junit.Assert.assertTrue;
36+
37+
/**
38+
* @author Vlad Mihalcea
39+
*/
40+
@RequiresDialect(H2Dialect.class)
41+
@TestForIssue(jiraKey = "HHH-8805")
42+
public class SchemaUpdateJoinColumnNoConstraintTest extends BaseUnitTestCase {
43+
44+
private static final String DELIMITER = ";";
45+
46+
@Test
47+
public void test() throws Exception {
48+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder()
49+
.applySetting( Environment.HBM2DDL_AUTO, "none" )
50+
.build();
51+
try {
52+
File output = File.createTempFile( "update_script", ".sql" );
53+
output.deleteOnExit();
54+
55+
final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
56+
.addAnnotatedClass( Parent.class )
57+
.addAnnotatedClass( Child.class )
58+
.buildMetadata();
59+
metadata.validate();
60+
61+
new SchemaUpdate()
62+
.setHaltOnError( true )
63+
.setOutputFile( output.getAbsolutePath() )
64+
.setDelimiter( DELIMITER )
65+
.setFormat( true )
66+
.execute( EnumSet.of( TargetType.SCRIPT ), metadata );
67+
68+
String outputContent = new String(Files.readAllBytes(output.toPath()));
69+
70+
assertFalse( outputContent.toLowerCase().contains( "foreign key" ) );
71+
72+
}
73+
finally {
74+
StandardServiceRegistryBuilder.destroy( ssr );
75+
}
76+
}
77+
78+
@Entity(name = "Parent")
79+
public static class Parent {
80+
81+
@Id
82+
private Long id;
83+
84+
public Long getId() {
85+
return id;
86+
}
87+
88+
public void setId(Long id) {
89+
this.id = id;
90+
}
91+
}
92+
93+
@Entity(name = "Child")
94+
public static class Child {
95+
96+
@Id
97+
private Long id;
98+
99+
@ManyToOne
100+
@JoinColumn(
101+
name = "some_fk",
102+
foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
103+
)
104+
private Parent parent;
105+
}
106+
107+
}

0 commit comments

Comments
 (0)