diff --git a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd index 7b7fc94cc7..e132bc934f 100644 --- a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/legacy-mapping-4.0.xsd @@ -964,7 +964,7 @@ - + @@ -1014,7 +1014,7 @@ - + @@ -1046,7 +1046,7 @@ - + diff --git a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/mapping-7.0.xsd b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/mapping-7.0.xsd index c7c0d9b2e1..6e9362ae48 100644 --- a/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/mapping-7.0.xsd +++ b/hibernate-core/src/main/resources/org/hibernate/xsd/mapping/mapping-7.0.xsd @@ -1620,7 +1620,7 @@ - + @@ -1665,7 +1665,7 @@ - + @@ -2046,7 +2046,7 @@ - + diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/Customer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/Customer.java new file mode 100644 index 0000000000..a19acae2e1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/Customer.java @@ -0,0 +1,34 @@ +package org.hibernate.orm.test.schemaupdate.foreignkeys; + +import java.util.List; + +public class Customer { + + private Long id; + private String name; + List inventory; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getInventory() { + return inventory; + } + + public void setInventory(List inventory) { + this.inventory = inventory; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/CustomerInventory.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/CustomerInventory.java new file mode 100644 index 0000000000..9f76425f52 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/CustomerInventory.java @@ -0,0 +1,31 @@ +package org.hibernate.orm.test.schemaupdate.foreignkeys; + +public class CustomerInventory { + private Long id; + private String name; + private Customer customer; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/ForeignKeysCreationForXMLMappingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/ForeignKeysCreationForXMLMappingTest.java new file mode 100644 index 0000000000..f22915208c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/ForeignKeysCreationForXMLMappingTest.java @@ -0,0 +1,63 @@ +package org.hibernate.orm.test.schemaupdate.foreignkeys; + +import java.io.File; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.util.EnumSet; +import java.util.List; + +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.dialect.H2Dialect; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.schema.TargetType; + +import org.hibernate.testing.orm.junit.BaseUnitTest; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.util.ServiceRegistryUtil; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@BaseUnitTest +@RequiresDialect(H2Dialect.class) +public class ForeignKeysCreationForXMLMappingTest { + + @Test + public void testForeignKeyCreation() throws Exception { + File output = File.createTempFile( "person_fk_script", ".sql" ); + output.deleteOnExit(); + StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry(); + try { + final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addResource( "org/hibernate/orm/test/schemaupdate/foreignkeys/customer.orm.xml" ) + .buildMetadata(); + metadata.validate(); + + new SchemaExport().setOutputFile( output.getAbsolutePath() ).createOnly( + EnumSet.of( TargetType.SCRIPT ), + metadata + ); + + final List sqlLines = Files.readAllLines( output.toPath(), Charset.defaultCharset() ); + assertThat( sqlLines.size() ).isNotEqualTo( 0 ); + assertTrue( checkFKCreation( sqlLines ), "Foreign keys have not been created" ); + } + finally { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + private boolean checkFKCreation(List sqlLines) { + for ( String sql : sqlLines ) { + if ( sql.contains( "foreign key" ) ) { + return true; + } + } + return false; + } + +} diff --git a/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/foreignkeys/customer.orm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/foreignkeys/customer.orm.xml new file mode 100644 index 0000000000..da0234a7f8 --- /dev/null +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/schemaupdate/foreignkeys/customer.orm.xml @@ -0,0 +1,30 @@ + + + + org.hibernate.orm.test.schemaupdate.foreignkeys + + + + + + + + + + + + + + + + + + +