From 3e00630808044fa7b744d7bf3505e5430d80589c Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Fri, 11 Mar 2016 09:06:35 -0600 Subject: [PATCH] HHH-9693 - H2 : Hibernate cannot validate it's own created schema --- .../java/org/hibernate/dialect/H2Dialect.java | 3 +- .../LongVarcharValidationTest.java | 163 ++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/schemavalidation/LongVarcharValidationTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java index 19d3d0e8f9..43488bd4ee 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2Dialect.java @@ -116,7 +116,8 @@ public class H2Dialect extends Dialect { registerColumnType( Types.FLOAT, "float" ); registerColumnType( Types.INTEGER, "integer" ); registerColumnType( Types.LONGVARBINARY, "longvarbinary" ); - registerColumnType( Types.LONGVARCHAR, "longvarchar" ); + // H2 does define "longvarchar", but it is a simple alias to "varchar" + registerColumnType( Types.LONGVARCHAR, String.format( "varchar(%d)", Integer.MAX_VALUE ) ); registerColumnType( Types.REAL, "real" ); registerColumnType( Types.SMALLINT, "smallint" ); registerColumnType( Types.TINYINT, "tinyint" ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/LongVarcharValidationTest.java b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/LongVarcharValidationTest.java new file mode 100644 index 0000000000..6c52bda034 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/schemavalidation/LongVarcharValidationTest.java @@ -0,0 +1,163 @@ +/* + * 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.schemavalidation; + +import java.util.EnumSet; +import java.util.Map; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.annotations.Type; +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.engine.config.spi.ConfigurationService; +import org.hibernate.tool.schema.SourceType; +import org.hibernate.tool.schema.TargetType; +import org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl; +import org.hibernate.tool.schema.spi.ExceptionHandler; +import org.hibernate.tool.schema.spi.ExecutionOptions; +import org.hibernate.tool.schema.spi.SchemaManagementTool; +import org.hibernate.tool.schema.spi.ScriptSourceInput; +import org.hibernate.tool.schema.spi.ScriptTargetOutput; +import org.hibernate.tool.schema.spi.SourceDescriptor; +import org.hibernate.tool.schema.spi.TargetDescriptor; + +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-9693" ) +public class LongVarcharValidationTest extends BaseUnitTestCase implements ExecutionOptions { + private StandardServiceRegistry ssr; + + @Before + public void beforeTest() { + ssr = new StandardServiceRegistryBuilder().build(); + } + + @After + public void afterTest() { + if ( ssr != null ) { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + @Test + public void testValidation() { + MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) + .addAnnotatedClass( Translation.class ) + .buildMetadata(); + metadata.validate(); + + + // create the schema + createSchema( metadata ); + + try { + doValidation( metadata ); + } + finally { + dropSchema( metadata ); + } + } + + private void doValidation(MetadataImplementor metadata) { + ssr.getService( SchemaManagementTool.class ).getSchemaValidator( null ).doValidation( + metadata, + this + ); + } + + private void createSchema(MetadataImplementor metadata) { + ssr.getService( SchemaManagementTool.class ).getSchemaCreator( null ).doCreation( + metadata, + this, + new SourceDescriptor() { + @Override + public SourceType getSourceType() { + return SourceType.METADATA; + } + + @Override + public ScriptSourceInput getScriptSourceInput() { + return null; + } + }, + new TargetDescriptor() { + @Override + public EnumSet getTargetTypes() { + return EnumSet.of( TargetType.DATABASE ); + } + + @Override + public ScriptTargetOutput getScriptTargetOutput() { + return null; + } + } + ); + } + + private void dropSchema(MetadataImplementor metadata) { + ssr.getService( SchemaManagementTool.class ).getSchemaDropper( null ).doDrop( + metadata, + this, + new SourceDescriptor() { + @Override + public SourceType getSourceType() { + return SourceType.METADATA; + } + + @Override + public ScriptSourceInput getScriptSourceInput() { + return null; + } + }, + new TargetDescriptor() { + @Override + public EnumSet getTargetTypes() { + return EnumSet.of( TargetType.DATABASE ); + } + + @Override + public ScriptTargetOutput getScriptTargetOutput() { + return null; + } + } + ); + } + + @Entity + public static class Translation { + @Id + public Integer id; + @Type( type = "text" ) + String text; + } + + @Override + public Map getConfigurationValues() { + return ssr.getService( ConfigurationService.class ).getSettings(); + } + + @Override + public boolean shouldManageNamespaces() { + return false; + } + + @Override + public ExceptionHandler getExceptionHandler() { + return ExceptionHandlerLoggedImpl.INSTANCE; + } +}