From 107849c3c81c8a1fd7242a1cc51104062890c0b2 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Wed, 16 Jun 2021 08:37:14 +0200 Subject: [PATCH 1/4] Fix jakarta sources transformation issues --- hibernate-core/hibernate-core.gradle | 4 ++++ hibernate-envers/hibernate-envers.gradle | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/hibernate-core/hibernate-core.gradle b/hibernate-core/hibernate-core.gradle index 069335b2b7..c04f082d21 100644 --- a/hibernate-core/hibernate-core.gradle +++ b/hibernate-core/hibernate-core.gradle @@ -229,6 +229,10 @@ task copyBundleResources (type: Copy) { } processTestResources.dependsOn copyBundleResources +sourcesJar { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + task testJar(type: Jar, dependsOn: testClasses) { duplicatesStrategy = DuplicatesStrategy.EXCLUDE archiveClassifier.set( 'test' ) diff --git a/hibernate-envers/hibernate-envers.gradle b/hibernate-envers/hibernate-envers.gradle index 5bcc7d4e9f..005453e918 100644 --- a/hibernate-envers/hibernate-envers.gradle +++ b/hibernate-envers/hibernate-envers.gradle @@ -71,6 +71,10 @@ jar { } } +sourcesJar { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + task testJar(type: Jar, dependsOn: testClasses) { duplicatesStrategy = DuplicatesStrategy.EXCLUDE archiveClassifier.set( 'test' ) From 8fc45a90044f4e6e54e1a03f1d3b074253554918 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 16 Jun 2021 16:03:50 +0200 Subject: [PATCH 2/4] HHH-11817 Allow schema-export commands written to file to truncate in addition to current appending --- .../userguide/appendices/Configurations.adoc | 4 +++ .../org/hibernate/cfg/AvailableSettings.java | 10 ++++++ .../hibernate/tool/hbm2ddl/SchemaExport.java | 31 +++++++++++++++++-- .../hibernate/tool/hbm2ddl/SchemaUpdate.java | 15 ++++++++- .../tool/schema/internal/Helper.java | 5 +-- .../exec/ScriptTargetOutputToFile.java | 29 ++++++++++++----- .../exec/ScriptTargetOutputToUrl.java | 24 +++++++++++--- .../spi/SchemaManagementToolCoordinator.java | 28 +++++++++++------ 8 files changed, 120 insertions(+), 26 deletions(-) diff --git a/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc b/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc index 3f608e0b55..d896314c5c 100644 --- a/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc +++ b/documentation/src/main/asciidoc/userguide/appendices/Configurations.adoc @@ -887,6 +887,10 @@ For cases where the `javax.persistence.schema-generation.scripts.action` value i `*javax.persistence.schema-generation.scripts.drop-target*`:: For cases where the `javax.persistence.schema-generation.scripts.action` value indicates that schema dropping commands should be written to DDL script file, `javax.persistence.schema-generation.scripts.drop-target` specifies either a `java.io.Writer` configured for output of the DDL script or a string specifying the file URL for the DDL script. +`*hibernate.hbm2ddl.schema-generation.script.append*` (e.g. `true` (default value) or `false`):: +For cases where the `javax.persistence.schema-generation.scripts.action` value indicates that schema commands should be written to DDL script file, `hibernate.hbm2ddl.schema-generation.script.append` specifies if schema commands should be appended to the end of the file rather than written at the beginning of the file. +Values are `true` for appending schema commands to the end of the file, `false` for writing achema commands at the beginning of the file. + `*javax.persistence.hibernate.hbm2ddl.import_files*` (e.g. `import.sql` (default value)):: Comma-separated names of the optional files containing SQL DML statements executed during the `SessionFactory` creation. File order matters, the statements of a given file are executed before the statements of the following one. diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java index a40320d3b7..13cb5b8a6f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AvailableSettings.java @@ -1588,6 +1588,16 @@ public interface AvailableSettings extends org.hibernate.jpa.AvailableSettings { @SuppressWarnings("JavaDoc") String HBM2DDL_SCRIPTS_CREATE_TARGET = "javax.persistence.schema-generation.scripts.create-target"; + /** + * For cases where the {@value #HBM2DDL_SCRIPTS_ACTION} value indicates that schema commands should + * be written to DDL script file, specifies if schema commands should be appended to the end of the file rather than written at the beginning of the file. + * + * Values are: {@code true} for appending schema commands to the end of the file, {@code false} for writing schema commands at the beginning. + * + * The default value is {@code true} + */ + String HBM2DDL_SCRIPTS_CREATE_APPEND = "hibernate.hbm2ddl.schema-generation.script.append"; + /** * For cases where the {@value #HBM2DDL_SCRIPTS_ACTION} value indicates that schema drop commands should * be written to DDL script file, {@value #HBM2DDL_SCRIPTS_DROP_TARGET} specifies either a diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java index f80bb8c3f5..33d3e68094 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaExport.java @@ -135,6 +135,7 @@ else if ( actionText.equalsIgnoreCase( "drop-and-create" ) ) { } } + boolean append = true; boolean haltOnError = false; boolean format = false; boolean manageNamespaces = false; @@ -159,6 +160,18 @@ public SchemaExport setOutputFile(String filename) { return this; } + /** + * For generating a export script file, by default the content will be appended at the begin or end of the file. + * + * The sql will be written at the beginning of the file rather append to the end. + * + * @return this + */ + public SchemaExport setOverrideOutputFileContent() { + append = false; + return this; + } + /** * Comma-separated list of resource names to use for database init commands on create. * @@ -244,7 +257,12 @@ public void execute(EnumSet targetTypes, Action action, Metadata met LOG.runningHbm2ddlSchemaExport(); - final TargetDescriptor targetDescriptor = buildTargetDescriptor( targetTypes, outputFile, serviceRegistry ); + final TargetDescriptor targetDescriptor = buildTargetDescriptor( + targetTypes, + outputFile, + append, + serviceRegistry + ); doExecution( action, needsJdbcConnection( targetTypes ), metadata, serviceRegistry, targetDescriptor ); } @@ -316,6 +334,14 @@ public static TargetDescriptor buildTargetDescriptor( EnumSet targetTypes, String outputFile, ServiceRegistry serviceRegistry) { + return buildTargetDescriptor( targetTypes, outputFile, true, serviceRegistry ); + } + + public static TargetDescriptor buildTargetDescriptor( + EnumSet targetTypes, + String outputFile, + boolean append, + ServiceRegistry serviceRegistry) { final ScriptTargetOutput scriptTarget; if ( targetTypes.contains( TargetType.SCRIPT ) ) { if ( outputFile == null ) { @@ -324,7 +350,8 @@ public static TargetDescriptor buildTargetDescriptor( scriptTarget = Helper.interpretScriptTargetSetting( outputFile, serviceRegistry.getService( ClassLoaderService.class ), - (String) serviceRegistry.getService( ConfigurationService.class ).getSettings().get( AvailableSettings.HBM2DDL_CHARSET_NAME ) + (String) serviceRegistry.getService( ConfigurationService.class ).getSettings().get( AvailableSettings.HBM2DDL_CHARSET_NAME ), + append ); } else { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java index 1b4a8768ae..4fe27c453e 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/hbm2ddl/SchemaUpdate.java @@ -55,6 +55,7 @@ public class SchemaUpdate { boolean haltOnError = false; private String outputFile; + private boolean append = true; private String delimiter; private boolean format; @@ -87,7 +88,7 @@ public void execute(EnumSet targetTypes, Metadata metadata, ServiceR exceptionHandler ); - final TargetDescriptor targetDescriptor = SchemaExport.buildTargetDescriptor( targetTypes, outputFile, serviceRegistry ); + final TargetDescriptor targetDescriptor = SchemaExport.buildTargetDescriptor( targetTypes, outputFile, append, serviceRegistry ); try { tool.getSchemaMigrator( config ).doMigration( metadata, executionOptions, targetDescriptor ); @@ -123,6 +124,18 @@ public SchemaUpdate setOutputFile(String outputFile) { return this; } + /** + * For generating a export script file, by default the content will be appended at the begin or end of the file. + * + * The sql will be written at the beginning of the file rather append to the end. + * + * @return this + */ + public SchemaUpdate setOverrideOutputFileContent() { + append = false; + return this; + } + /** * Set the end of statement delimiter * diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java index 49c66cff17..f4513baccd 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/Helper.java @@ -93,7 +93,8 @@ private static ScriptSourceInput interpretScriptSourceSetting( public static ScriptTargetOutput interpretScriptTargetSetting( Object scriptTargetSetting, ClassLoaderService classLoaderService, - String charsetName ) { + String charsetName, + boolean append) { if ( scriptTargetSetting == null ) { return null; } @@ -118,7 +119,7 @@ else if ( Writer.class.isInstance( scriptTargetSetting ) ) { // assume it is a File path final File file = new File( scriptTargetSettingString ); - return new ScriptTargetOutputToFile( file, charsetName ); + return new ScriptTargetOutputToFile( file, charsetName, append ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java index 3338377500..abdc3309ef 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToFile.java @@ -28,6 +28,7 @@ public class ScriptTargetOutputToFile extends AbstractScriptTargetOutput impleme private final File file; private final String charsetName; + private final boolean append; private Writer writer; @@ -36,10 +37,24 @@ public class ScriptTargetOutputToFile extends AbstractScriptTargetOutput impleme * * @param file The file to read from * @param charsetName The charset name + * @param append If true, then bytes will be written to the end of the file rather than the beginning */ - public ScriptTargetOutputToFile(File file, String charsetName) { + public ScriptTargetOutputToFile(File file, String charsetName, boolean append) { this.file = file; this.charsetName = charsetName; + this.append = append; + } + + /** + * Constructs a ScriptTargetOutputToFile instance, + * the bytes will be written to the end of the file rather than the beginning + * + * @param file The file to read from + * @param charsetName The charset name + * + */ + public ScriptTargetOutputToFile(File file, String charsetName ) { + this(file, charsetName, true); } @Override @@ -53,7 +68,7 @@ protected Writer writer() { @Override public void prepare() { super.prepare(); - this.writer = toFileWriter( this.file, this.charsetName ); + this.writer = toFileWriter( this.file, this.charsetName, append ); } @Override @@ -63,7 +78,7 @@ public void release() { writer.close(); } catch (IOException e) { - throw new SchemaManagementException( "Unable to close file writer : " + e.toString() ); + throw new SchemaManagementException( "Unable to close file writer : " + e ); } finally { writer = null; @@ -72,7 +87,7 @@ public void release() { } @SuppressWarnings("ResultOfMethodCallIgnored") - static Writer toFileWriter( File file, String charsetName ) { + static Writer toFileWriter(File file, String charsetName, boolean append) { try { if ( ! file.exists() ) { // best effort, since this is very likely not allowed in EE environments @@ -84,17 +99,17 @@ static Writer toFileWriter( File file, String charsetName ) { } } catch (Exception e) { - log.debug( "Exception calling File#createNewFile : " + e.toString() ); + log.debug( "Exception calling File#createNewFile : " + e ); } try { return charsetName != null ? new OutputStreamWriter( - new FileOutputStream( file, true ), + new FileOutputStream( file, append ), charsetName ) : new OutputStreamWriter( new FileOutputStream( file, - true + append ) ); } catch (IOException e) { diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java index 02fca12742..f342efb613 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/internal/exec/ScriptTargetOutputToUrl.java @@ -28,6 +28,7 @@ public class ScriptTargetOutputToUrl extends AbstractScriptTargetOutput implemen private final URL url; private final String charsetName; + private final boolean append; private Writer writer; @@ -36,10 +37,23 @@ public class ScriptTargetOutputToUrl extends AbstractScriptTargetOutput implemen * * @param url The url to read from * @param charsetName The charset name + * @param append If true, then bytes will be written to the end of the file rather than the beginning */ - public ScriptTargetOutputToUrl(URL url, String charsetName) { + public ScriptTargetOutputToUrl(URL url, String charsetName, boolean append) { this.url = url; this.charsetName = charsetName; + this.append = append; + } + + /** + * Constructs a ScriptTargetOutputToUrl instance + * the bytes will be written to the end of the file rather than the beginning + * + * @param url The url to read from + * @param charsetName The charset name + */ + public ScriptTargetOutputToUrl(URL url, String charsetName) { + this( url, charsetName, true ); } @Override @@ -53,7 +67,7 @@ protected Writer writer() { @Override public void prepare() { super.prepare(); - this.writer = toWriter( url, charsetName ); + this.writer = toWriter( url, charsetName, append ); } @Override @@ -62,17 +76,17 @@ public void release() { writer().close(); } catch (IOException e) { - throw new SchemaManagementException( "Unable to close file writer : " + e.toString() ); + throw new SchemaManagementException( "Unable to close file writer : " + e ); } } - private static Writer toWriter( URL url, String charsetName ) { + private static Writer toWriter( URL url, String charsetName, boolean append ) { log.debug( "Attempting to resolve writer for URL : " + url ); // technically only "strings corresponding to file URLs" are supported, which I take to mean URLs whose // protocol is "file" try { - return ScriptTargetOutputToFile.toFileWriter( new File( url.toURI() ), charsetName ); + return ScriptTargetOutputToFile.toFileWriter( new File( url.toURI() ), charsetName, append ); } catch (URISyntaxException e) { throw new SchemaManagementException( diff --git a/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java b/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java index d0d27aaccf..4c0c33807c 100644 --- a/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java +++ b/hibernate-core/src/main/java/org/hibernate/tool/schema/spi/SchemaManagementToolCoordinator.java @@ -77,7 +77,7 @@ public static void process( ExceptionHandlerLoggedImpl.INSTANCE ); - performScriptAction( actions.getScriptAction(), metadata, tool, serviceRegistry, executionOptions ); + performScriptAction( actions.getScriptAction(), metadata, tool, serviceRegistry, executionOptions, configService ); performDatabaseAction( actions.getDatabaseAction(), metadata, tool, serviceRegistry, executionOptions ); if ( actions.getDatabaseAction() == Action.CREATE_DROP ) { @@ -260,13 +260,15 @@ private static void performScriptAction( Metadata metadata, SchemaManagementTool tool, ServiceRegistry serviceRegistry, - ExecutionOptions executionOptions) { + ExecutionOptions executionOptions, + ConfigurationService configurationService) { switch ( scriptAction ) { case CREATE_ONLY: { final JpaTargetAndSourceDescriptor createDescriptor = buildScriptTargetDescriptor( executionOptions.getConfigurationValues(), CreateSettingSelector.INSTANCE, - serviceRegistry + serviceRegistry, + configurationService ); tool.getSchemaCreator( executionOptions.getConfigurationValues() ).doCreation( metadata, @@ -281,7 +283,8 @@ private static void performScriptAction( final JpaTargetAndSourceDescriptor dropDescriptor = buildScriptTargetDescriptor( executionOptions.getConfigurationValues(), DropSettingSelector.INSTANCE, - serviceRegistry + serviceRegistry, + configurationService ); tool.getSchemaDropper( executionOptions.getConfigurationValues() ).doDrop( metadata, @@ -292,7 +295,8 @@ private static void performScriptAction( final JpaTargetAndSourceDescriptor createDescriptor = buildScriptTargetDescriptor( executionOptions.getConfigurationValues(), CreateSettingSelector.INSTANCE, - serviceRegistry + serviceRegistry, + configurationService ); tool.getSchemaCreator( executionOptions.getConfigurationValues() ).doCreation( metadata, @@ -306,7 +310,8 @@ private static void performScriptAction( final JpaTargetAndSourceDescriptor dropDescriptor = buildScriptTargetDescriptor( executionOptions.getConfigurationValues(), DropSettingSelector.INSTANCE, - serviceRegistry + serviceRegistry, + configurationService ); tool.getSchemaDropper( executionOptions.getConfigurationValues() ).doDrop( metadata, @@ -320,7 +325,8 @@ private static void performScriptAction( final JpaTargetAndSourceDescriptor migrateDescriptor = buildScriptTargetDescriptor( executionOptions.getConfigurationValues(), MigrateSettingSelector.INSTANCE, - serviceRegistry + serviceRegistry, + configurationService ); tool.getSchemaMigrator( executionOptions.getConfigurationValues() ).doMigration( metadata, @@ -338,7 +344,8 @@ private static void performScriptAction( private static JpaTargetAndSourceDescriptor buildScriptTargetDescriptor( Map configurationValues, SettingSelector settingSelector, - ServiceRegistry serviceRegistry) { + ServiceRegistry serviceRegistry, + ConfigurationService configurationService) { final Object scriptSourceSetting = settingSelector.getScriptSourceSetting( configurationValues ); final SourceType sourceType = SourceType.interpret( settingSelector.getSourceTypeSetting( configurationValues ), @@ -358,10 +365,13 @@ private static JpaTargetAndSourceDescriptor buildScriptTargetDescriptor( ? Helper.interpretScriptSourceSetting( scriptSourceSetting, serviceRegistry.getService( ClassLoaderService.class ), charsetName ) : null; + + boolean append = configurationService.getSetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_APPEND, StandardConverters.BOOLEAN, true ); final ScriptTargetOutput scriptTargetOutput = Helper.interpretScriptTargetSetting( settingSelector.getScriptTargetSetting( configurationValues ), serviceRegistry.getService( ClassLoaderService.class ), - charsetName + charsetName, + append ); return new JpaTargetAndSourceDescriptor() { From bdc08af163df6bcc0dfaf6483d961ee650123ee0 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 16 Jun 2021 16:04:10 +0200 Subject: [PATCH 3/4] HHH-11817 Add test for issue --- .../SchemaCreationToOutputScriptTest.java | 170 ++++++++++++++++ .../SchemaDropToOutputScriptTest.java | 170 ++++++++++++++++ .../SchemaMigrationToOutputScriptTest.java | 188 ++++++++++++++++++ 3 files changed, 528 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaCreationToOutputScriptTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaDropToOutputScriptTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaMigrationToOutputScriptTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaCreationToOutputScriptTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaCreationToOutputScriptTest.java new file mode 100644 index 0000000000..584dde20f4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaCreationToOutputScriptTest.java @@ -0,0 +1,170 @@ +/* + * 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.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.StringContains.containsString; + +@TestForIssue(jiraKey = "HHH-11817") +@RequiresDialect(H2Dialect.class) +public class SchemaCreationToOutputScriptTest extends BaseUnitTestCase { + + private final String createTableMyEntity = "create table MyEntity"; + private final String createTableMySecondEntity = "create table MySecondEntity"; + + private File output; + private ServiceRegistry serviceRegistry; + private MetadataImplementor metadata; + + @Before + public void setUp() throws Exception { + output = File.createTempFile( "creation_script", ".sql" ); + output.deleteOnExit(); + + List content = Arrays.asList( + "This is the database creation script generated by Hibernate" + , "This is the second line" + , "This is the third line" + , "This is the fourth line" + , "This is the fifth line" + , "This is the sixth line" + , "This is the seventh line" + , "This is the eighth line" + , "This is the ninth line" + ); + + try (BufferedWriter bw = new BufferedWriter( new FileWriter( output ) )) { + for ( String s : content ) { + bw.write( s ); + bw.write( System.lineSeparator() ); + } + } + } + + private void createServiceRegistryAndMetadata(String append) { + final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder() + .applySetting( Environment.FORMAT_SQL, "false" ) + .applySetting( Environment.HBM2DDL_SCRIPTS_ACTION, "create" ) + .applySetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, output.getAbsolutePath() ); + if ( append != null ) { + standardServiceRegistryBuilder.applySetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_APPEND, append ); + } + + serviceRegistry = standardServiceRegistryBuilder.build(); + + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( MyEntity.class ) + .addAnnotatedClass( MySecondEntity.class ) + .buildMetadata(); + metadata.validate(); + } + + @After + public void tearDown() { + ServiceRegistryBuilder.destroy( serviceRegistry ); + } + + @Test + public void testAppendModeFalse() throws Exception { + createServiceRegistryAndMetadata( "false" ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 2 ) ); + assertThat( commands.get( 0 ), containsString( createTableMyEntity ) ); + assertThat( commands.get( 1 ), containsString( createTableMySecondEntity ) ); + } + + @Test + public void testAppendModeTrue() throws Exception { + createServiceRegistryAndMetadata( "true" ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 11 ) ); + assertThat( commands.get( 9 ), containsString( createTableMyEntity ) ); + assertThat( commands.get( 10 ), containsString( createTableMySecondEntity ) ); + } + + @Test + public void testDefaultAppendMode() throws Exception { + createServiceRegistryAndMetadata( null ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 11 ) ); + assertThat( commands.get( 9 ), containsString( createTableMyEntity ) ); + assertThat( commands.get( 10 ), containsString( createTableMySecondEntity ) ); + } + + @Entity(name = "MyEntity") + public static class MyEntity { + @Id + private Long id; + + private String name; + } + + @Entity(name = "MySecondEntity") + public static class MySecondEntity { + @Id + private Long id; + + private String name; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaDropToOutputScriptTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaDropToOutputScriptTest.java new file mode 100644 index 0000000000..d5de59440a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaDropToOutputScriptTest.java @@ -0,0 +1,170 @@ +/* + * 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.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@TestForIssue(jiraKey = "HHH-11817") +@RequiresDialect(H2Dialect.class) +public class SchemaDropToOutputScriptTest extends BaseUnitTestCase { + + private File output; + private ServiceRegistry serviceRegistry; + private MetadataImplementor metadata; + private final String dropMyEntityTable = "drop table if exists MyEntity"; + private final String dropMySecondEntityTable = "drop table if exists MySecondEntity"; + + + @Before + public void setUp() throws Exception { + output = File.createTempFile( "creation_script", ".sql" ); + output.deleteOnExit(); + List content = Arrays.asList( + "This is the database creation script generated by Hibernate" + , "This is the second line" + , "This is the third line" + , "This is the fourth line" + , "This is the fifth line" + , "This is the sixth line" + , "This is the seventh line" + , "This is the eighth line" + , "This is the ninth line" + ); + + try (BufferedWriter bw = new BufferedWriter( new FileWriter( output ) )) { + for ( String s : content ) { + bw.write( s ); + bw.write( System.lineSeparator() ); + } + } + } + + private void createServiceRegistryAndMetadata(String append) { + final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder() + .applySetting( Environment.FORMAT_SQL, "false" ) + .applySetting( Environment.HBM2DDL_SCRIPTS_ACTION, "drop" ) + .applySetting( AvailableSettings.HBM2DDL_SCRIPTS_DROP_TARGET, output.getAbsolutePath() ); + + if ( append != null ) { + standardServiceRegistryBuilder.applySetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_APPEND, append ); + } + + serviceRegistry = standardServiceRegistryBuilder.build(); + + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( MyEntity.class ) + .addAnnotatedClass( MySecondEntity.class ) + .buildMetadata(); + metadata.validate(); + } + + @After + public void tearDown() { + ServiceRegistryBuilder.destroy( serviceRegistry ); + } + + @Test + public void testAppendModeFalse() throws Exception { + createServiceRegistryAndMetadata( "false" ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 2 ) ); + assertThat( commands.get( 0 ), containsString( dropMyEntityTable ) ); + assertThat( commands.get( 1 ), containsString( dropMySecondEntityTable ) ); + } + + @Test + public void testAppendModeTrue() throws Exception { + createServiceRegistryAndMetadata( "true" ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 11 ) ); + assertThat( commands.get( 9 ), containsString( dropMyEntityTable ) ); + assertThat( commands.get( 10 ), containsString( dropMySecondEntityTable ) ); + } + + @Test + public void testDefaultAppendMode() throws Exception { + createServiceRegistryAndMetadata( null ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 11 ) ); + assertThat( commands.get( 9 ), containsString( dropMyEntityTable ) ); + assertThat( commands.get( 10 ), containsString( dropMySecondEntityTable ) ); + } + + @Entity(name = "MyEntity") + public static class MyEntity { + @Id + private Long id; + + private String name; + } + + @Entity(name = "MySecondEntity") + public static class MySecondEntity { + @Id + private Long id; + + private String name; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaMigrationToOutputScriptTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaMigrationToOutputScriptTest.java new file mode 100644 index 0000000000..88d85f12ca --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemaupdate/SchemaMigrationToOutputScriptTest.java @@ -0,0 +1,188 @@ +/* + * 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.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.boot.spi.MetadataImplementor; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.config.spi.ConfigurationService; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.ServiceRegistryBuilder; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@TestForIssue(jiraKey = "HHH-11817") +@RequiresDialect(H2Dialect.class) +public class SchemaMigrationToOutputScriptTest extends BaseUnitTestCase { + + private final String createTableMySecondEntity = "create table MySecondEntity"; + + private File output; + private ServiceRegistry serviceRegistry; + private MetadataImplementor metadata; + + @Before + public void setUp() throws Exception { + output = File.createTempFile( "creation_script", ".sql" ); + output.deleteOnExit(); + + List content = Arrays.asList( + "This is the database creation script generated by Hibernate" + , "This is the second line" + , "This is the third line" + , "This is the fourth line" + , "This is the fifth line" + , "This is the sixth line" + , "This is the seventh line" + , "This is the eighth line" + , "This is the ninth line" + ); + + try (BufferedWriter bw = new BufferedWriter( new FileWriter( output ) )) { + for ( String s : content ) { + bw.write( s ); + bw.write( System.lineSeparator() ); + } + } + + serviceRegistry = new StandardServiceRegistryBuilder().applySetting( + AvailableSettings.HBM2DDL_AUTO, + "create-only" + ) + .build(); + + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( MyEntity.class ) + .buildMetadata(); + final SessionFactory sessionFactory = metadata.buildSessionFactory(); + sessionFactory.close(); + } + + private void createServiceRegistryAndMetadata(String append) { + final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder() + .applySetting( Environment.FORMAT_SQL, "false" ) + .applySetting( Environment.HBM2DDL_SCRIPTS_ACTION, "update" ) + .applySetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, output.getAbsolutePath() ); + + if ( append != null ) { + standardServiceRegistryBuilder.applySetting( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_APPEND, append ); + } + + serviceRegistry = standardServiceRegistryBuilder.build(); + + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( MyEntity.class ) + .addAnnotatedClass( MySecondEntity.class ) + .buildMetadata(); + metadata.validate(); + } + + @After + public void tearDown() { + ServiceRegistryBuilder.destroy( serviceRegistry ); + serviceRegistry = new StandardServiceRegistryBuilder().applySetting( AvailableSettings.HBM2DDL_AUTO, "drop" ) + .build(); + + metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) + .addAnnotatedClass( MyEntity.class ) + .buildMetadata(); + final SessionFactory sessionFactory = metadata.buildSessionFactory(); + sessionFactory.close(); + } + + @Test + public void testAppendModeFalse() throws Exception { + createServiceRegistryAndMetadata( "false" ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 1 ) ); + assertThat( commands.get( 0 ), containsString( createTableMySecondEntity ) ); + } + + @Test + public void testAppendModeTrue() throws Exception { + createServiceRegistryAndMetadata( "true" ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 10 ) ); + assertThat( commands.get( 9 ), containsString( createTableMySecondEntity ) ); + } + + @Test + public void testDefaultAppendMode() throws Exception { + createServiceRegistryAndMetadata( null ); + HashMap properties = new HashMap<>(); + properties.putAll( serviceRegistry.getService( ConfigurationService.class ).getSettings() ); + + SchemaManagementToolCoordinator.process( + metadata, + serviceRegistry, + properties, + null + ); + List commands = Files.readAllLines( output.toPath() ); + assertThat( commands.size(), is( 10 ) ); + assertThat( commands.get( 9 ), containsString( createTableMySecondEntity ) ); + } + + @Entity(name = "MyEntity") + public static class MyEntity { + @Id + private Long id; + + private String name; + } + + @Entity(name = "MySecondEntity") + public static class MySecondEntity { + @Id + private Long id; + + private String name; + } + +} From 7329f444b5fd248ee671bb99f260d666c64e2d7e Mon Sep 17 00:00:00 2001 From: Karel Maesen Date: Fri, 18 Jun 2021 13:23:32 +0200 Subject: [PATCH 4/4] HHH-14654 Fix for schema validation bug --- .../cockroachdb/CockroachDB202SpatialDialect.java | 5 +++++ .../cockroachdb/CockroachSpatialDialectTrait.java | 5 +++++ .../dialect/postgis/PGSpatialDialectTrait.java | 11 +++++++++++ .../spatial/dialect/postgis/PostgisPG10Dialect.java | 8 ++++++++ .../spatial/dialect/postgis/PostgisPG82Dialect.java | 5 +++++ .../spatial/dialect/postgis/PostgisPG91Dialect.java | 6 ++++++ .../spatial/dialect/postgis/PostgisPG92Dialect.java | 6 ++++++ .../spatial/dialect/postgis/PostgisPG93Dialect.java | 6 ++++++ .../spatial/dialect/postgis/PostgisPG94Dialect.java | 6 ++++++ .../spatial/dialect/postgis/PostgisPG95Dialect.java | 5 +++++ .../spatial/dialect/postgis/PostgisPG9Dialect.java | 6 ++++++ .../spatial/dialect/postgis/PostgisSupport.java | 5 +++++ 12 files changed, 74 insertions(+) diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDB202SpatialDialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDB202SpatialDialect.java index d5ba27617a..31f9927e09 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDB202SpatialDialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachDB202SpatialDialect.java @@ -42,4 +42,9 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry delegateContributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( isSpatial( typeCode1 ) && isSpatial( typeCode2 ) ); + } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java index 17ffb69b7e..2df63848ff 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/cockroachdb/CockroachSpatialDialectTrait.java @@ -74,4 +74,9 @@ default boolean supports(SpatialFunction function) { return DELEGATE.supports( function ); } + + default boolean isSpatial(int typeCode) { + return DELEGATE.isSpatial( typeCode ); + } + } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGSpatialDialectTrait.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGSpatialDialectTrait.java index 2c6dd2edf1..5b7abcd1e1 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGSpatialDialectTrait.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGSpatialDialectTrait.java @@ -7,6 +7,8 @@ package org.hibernate.spatial.dialect.postgis; +import java.sql.Types; + import org.hibernate.spatial.SpatialDialect; import org.hibernate.spatial.SpatialFunction; import org.hibernate.spatial.dialect.SpatialFunctionsRegistry; @@ -125,4 +127,13 @@ default boolean supportsFiltering() { default boolean supports(SpatialFunction function) { return support.supports( function ); } + + /** + * Checks whether the typeCode is (potentially) the code for a spatial type + * @param typeCode the JDBC type code + * @return if the typecode corresponds with a spatialt type + */ + default boolean isSpatial(int typeCode){ + return support.isSpatial( typeCode ); + } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java index e7c9412b63..1d238bde31 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG10Dialect.java @@ -7,6 +7,7 @@ package org.hibernate.spatial.dialect.postgis; +import java.sql.Types; import java.util.Map; import org.hibernate.boot.model.TypeContributions; @@ -36,4 +37,11 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( isSpatial( typeCode1 ) && isSpatial( typeCode2 ) ); + } + + } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG82Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG82Dialect.java index f1fa67c919..9351bceabb 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG82Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG82Dialect.java @@ -48,6 +48,11 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( support.isSpatial( typeCode1 ) && support.isSpatial( typeCode2 ) ); + } @Override public String getSpatialRelateSQL(String columnName, int spatialRelation) { diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java index ca871af1df..f2577625f0 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG91Dialect.java @@ -48,6 +48,12 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( support.isSpatial( typeCode1 ) && support.isSpatial( typeCode2 ) ); + } + /** * Returns the SQL fragment for the SQL WHERE-clause when parsing * org.hibernatespatial.criterion.SpatialRelateExpressions diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java index c94d2e9021..e71f982f86 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG92Dialect.java @@ -48,6 +48,12 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( support.isSpatial( typeCode1 ) && support.isSpatial( typeCode2 ) ); + } + /** * Returns the SQL fragment for the SQL WHERE-clause when parsing * org.hibernatespatial.criterion.SpatialRelateExpressions diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG93Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG93Dialect.java index f8ac29a1a0..ee4ddd9885 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG93Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG93Dialect.java @@ -48,6 +48,12 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( support.isSpatial( typeCode1 ) && support.isSpatial( typeCode2 ) ); + } + /** * Returns the SQL fragment for the SQL WHERE-clause when parsing * org.hibernatespatial.criterion.SpatialRelateExpressions diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG94Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG94Dialect.java index c267cf6d8c..6c14a2afc2 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG94Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG94Dialect.java @@ -48,6 +48,12 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( support.isSpatial( typeCode1 ) && support.isSpatial( typeCode2 ) ); + } + /** * Returns the SQL fragment for the SQL WHERE-clause when parsing * org.hibernatespatial.criterion.SpatialRelateExpressions diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG95Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG95Dialect.java index 118584fac2..1c31d221ad 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG95Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG95Dialect.java @@ -42,4 +42,9 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( isSpatial( typeCode1 ) && isSpatial( typeCode2 ) ); + } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java index 9182f947f0..88fefa5205 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisPG9Dialect.java @@ -48,6 +48,12 @@ public void contributeTypes(TypeContributions typeContributions, ServiceRegistry support.contributeTypes( typeContributions, serviceRegistry ); } + @Override + public boolean equivalentTypes(int typeCode1, int typeCode2) { + return super.equivalentTypes( typeCode1, typeCode2 ) || + ( support.isSpatial( typeCode1 ) && support.isSpatial( typeCode2 ) ); + } + /** * Returns the SQL fragment for the SQL WHERE-clause when parsing * org.hibernatespatial.criterion.SpatialRelateExpressions diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java index 126a86e072..ef361f47f5 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PostgisSupport.java @@ -7,6 +7,7 @@ package org.hibernate.spatial.dialect.postgis; import java.io.Serializable; +import java.sql.Types; import org.hibernate.boot.model.TypeContributions; import org.hibernate.service.ServiceRegistry; @@ -47,6 +48,10 @@ public SpatialFunctionsRegistry functionsToRegister() { return postgisFunctions; } + public boolean isSpatial(int typeCode){ + return typeCode == Types.OTHER || typeCode == PGGeometryTypeDescriptor.INSTANCE_WKB_1.getSqlType(); + } + /** * Returns the SQL fragment for the SQL WHERE-clause when parsing * org.hibernatespatial.criterion.SpatialRelateExpressions