HHH-9693 - H2 : Hibernate cannot validate it's own created schema

This commit is contained in:
Steve Ebersole 2016-03-11 09:06:35 -06:00
parent 0756bbfccf
commit 3e00630808
2 changed files with 165 additions and 1 deletions

View File

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

View File

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