Downgrade H2 to 1.4.197 for testing and to allow usage of H2GIS

This commit is contained in:
Christian Beikov 2021-11-12 09:31:33 +01:00
parent 3f7536a94e
commit 60a3c08563
6 changed files with 73 additions and 43 deletions

View File

@ -10,31 +10,30 @@ import java.sql.Timestamp;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.junit.Test; import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialect;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(H2Dialect.class) // On H2 1.4.199+ CURRENT_TIMESTAMP returns a timestamp with timezone
public class EmbeddedIdDatabaseGeneratedValueTest extends BaseEntityManagerFunctionalTestCase { @RequiresDialect(value = H2Dialect.class, version = 104199)
@DomainModel(annotatedClasses = Event.class)
@Override @SessionFactory
protected Class<?>[] getAnnotatedClasses() { public class EmbeddedIdDatabaseGeneratedValueTest {
return new Class[] { Event.class };
}
@Test @Test
@TestForIssue(jiraKey = "HHH-13096") @TestForIssue(jiraKey = "HHH-13096")
public void test() { public void test(SessionFactoryScope scope) {
final EventId eventId = doInJPA( this::entityManagerFactory, entityManager -> { final EventId eventId = scope.fromTransaction( entityManager -> {
// On H2 1.4.199+ CURRENT_TIMESTAMP returns a timestamp with timezone
//tag::identifiers-composite-generated-database-example[] //tag::identifiers-composite-generated-database-example[]
OffsetDateTime currentTimestamp = (OffsetDateTime) entityManager OffsetDateTime currentTimestamp = (OffsetDateTime) entityManager
.createNativeQuery( .createNativeQuery(
@ -55,7 +54,7 @@ public class EmbeddedIdDatabaseGeneratedValueTest extends BaseEntityManagerFunct
return event.getId(); return event.getId();
} ); } );
doInJPA( this::entityManagerFactory, entityManager -> { scope.fromSession( entityManager -> {
Event event = entityManager.find( Event.class, eventId ); Event event = entityManager.find( Event.class, eventId );

View File

@ -13,7 +13,7 @@ ext {
junitVintageVersion = '5.7.1' junitVintageVersion = '5.7.1'
junit5Version = '5.7.1' junit5Version = '5.7.1'
h2Version = '1.4.200' h2Version = '1.4.197'
bytemanVersion = '4.0.16' //Compatible with JDK16 bytemanVersion = '4.0.16' //Compatible with JDK16
jnpVersion = '5.0.6.CR1' jnpVersion = '5.0.6.CR1'

View File

@ -126,28 +126,30 @@ public class H2Dialect extends Dialect {
// http://code.google.com/p/h2database/issues/detail?id=235 // http://code.google.com/p/h2database/issues/detail?id=235
getDefaultProperties().setProperty( AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, "true" ); getDefaultProperties().setProperty( AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, "true" );
registerColumnType( SqlTypes.ARRAY, "array" );
if ( version >= 104032 ) { if ( version >= 104032 ) {
this.sequenceInformationExtractor = version >= 104201 this.sequenceInformationExtractor = version >= 104201
? SequenceInformationExtractorLegacyImpl.INSTANCE ? SequenceInformationExtractorLegacyImpl.INSTANCE
: SequenceInformationExtractorH2DatabaseImpl.INSTANCE; : SequenceInformationExtractorH2DatabaseImpl.INSTANCE;
this.querySequenceString = "select * from INFORMATION_SCHEMA.SEQUENCES"; this.querySequenceString = "select * from INFORMATION_SCHEMA.SEQUENCES";
registerColumnType( Types.DECIMAL, "numeric($p,$s)" ); registerColumnType( Types.DECIMAL, "numeric($p,$s)" );
if ( version >= 104197 ) {
registerColumnType( SqlTypes.UUID, "uuid" );
registerColumnType( SqlTypes.GEOMETRY, "geometry" );
if ( version >= 104198 ) {
registerColumnType( SqlTypes.INTERVAL_SECOND, "interval second($p,$s)" );
}
}
} }
else { else {
this.sequenceInformationExtractor = SequenceInformationExtractorNoOpImpl.INSTANCE; this.sequenceInformationExtractor = SequenceInformationExtractorNoOpImpl.INSTANCE;
this.querySequenceString = null; this.querySequenceString = null;
}
if ( version < 200 ) { if ( version < 200 ) {
// prior to version 2.0, H2 reported NUMERIC columns as DECIMAL, // prior to version 2.0, H2 reported NUMERIC columns as DECIMAL,
// which caused problems for schema update tool // which caused problems for schema update tool
registerColumnType( Types.NUMERIC, "decimal($p,$s)" ); registerColumnType( Types.NUMERIC, "decimal($p,$s)" );
} }
}
registerColumnType( SqlTypes.UUID, "uuid" );
registerColumnType( SqlTypes.INTERVAL_SECOND, "interval second($p,$s)" );
registerColumnType( SqlTypes.GEOMETRY, "geometry" );
registerColumnType( SqlTypes.ARRAY, "array" );
} }
@Override @Override
@ -156,9 +158,14 @@ public class H2Dialect extends Dialect {
final JdbcTypeRegistry jdbcTypeRegistry = typeContributions.getTypeConfiguration() final JdbcTypeRegistry jdbcTypeRegistry = typeContributions.getTypeConfiguration()
.getJdbcTypeDescriptorRegistry(); .getJdbcTypeDescriptorRegistry();
if ( version >= 104197 ) {
jdbcTypeRegistry.addDescriptorIfAbsent( UUIDJdbcType.INSTANCE ); jdbcTypeRegistry.addDescriptorIfAbsent( UUIDJdbcType.INSTANCE );
if ( version >= 104198 ) {
jdbcTypeRegistry.addDescriptorIfAbsent( DurationIntervalSecondJdbcType.INSTANCE ); jdbcTypeRegistry.addDescriptorIfAbsent( DurationIntervalSecondJdbcType.INSTANCE );
} }
}
}
private static int parseBuildId(DialectResolutionInfo info) { private static int parseBuildId(DialectResolutionInfo info) {
final String databaseVersion = info.getDatabaseVersion(); final String databaseVersion = info.getDatabaseVersion();
@ -205,7 +212,9 @@ public class H2Dialect extends Dialect {
CommonFunctionFactory.dayOfWeekMonthYear( queryEngine ); CommonFunctionFactory.dayOfWeekMonthYear( queryEngine );
CommonFunctionFactory.weekQuarter( queryEngine ); CommonFunctionFactory.weekQuarter( queryEngine );
CommonFunctionFactory.daynameMonthname( queryEngine ); CommonFunctionFactory.daynameMonthname( queryEngine );
if ( useLocalTime ) {
CommonFunctionFactory.localtimeLocaltimestamp( queryEngine ); CommonFunctionFactory.localtimeLocaltimestamp( queryEngine );
}
CommonFunctionFactory.bitLength( queryEngine ); CommonFunctionFactory.bitLength( queryEngine );
CommonFunctionFactory.octetLength( queryEngine ); CommonFunctionFactory.octetLength( queryEngine );
CommonFunctionFactory.ascii( queryEngine ); CommonFunctionFactory.ascii( queryEngine );

View File

@ -6,12 +6,14 @@
*/ */
package org.hibernate.dialect.sequence; package org.hibernate.dialect.sequence;
import org.hibernate.MappingException;
/** /**
* Sequence support for {@link org.hibernate.dialect.H2Dialect}. * Sequence support for {@link org.hibernate.dialect.H2Dialect}.
* *
* @author Gavin King * @author Gavin King
*/ */
public final class H2SequenceSupport extends ANSISequenceSupport { public final class H2SequenceSupport implements SequenceSupport {
public static final SequenceSupport INSTANCE = new H2SequenceSupport(); public static final SequenceSupport INSTANCE = new H2SequenceSupport();
@ -21,12 +23,12 @@ public final class H2SequenceSupport extends ANSISequenceSupport {
} }
@Override @Override
public String getSequenceNextValString(String sequenceName) { public String getSelectSequenceNextValString(String sequenceName) {
return "call " + getSelectSequenceNextValString( sequenceName ); return sequenceName + ".nextval";
} }
@Override @Override
public String getSequencePreviousValString(String sequenceName) { public String getSelectSequencePreviousValString(String sequenceName) throws MappingException {
return "call " + getSelectSequencePreviousValString( sequenceName ); return sequenceName + ".currval";
} }
} }

View File

@ -112,6 +112,10 @@ public class JavaTypeRegistry implements JavaTypeDescriptorBaseline.BaselineTarg
performInjections( descriptor ); performInjections( descriptor );
} }
public <J> JavaType<J> findDescriptor(Type javaType) {
return (JavaType<J>) descriptorsByType.get( javaType );
}
public <J> JavaType<J> resolveDescriptor(Type javaType, Supplier<JavaType<J>> creator) { public <J> JavaType<J> resolveDescriptor(Type javaType, Supplier<JavaType<J>> creator) {
final JavaType<?> cached = descriptorsByType.get( javaType ); final JavaType<?> cached = descriptorsByType.get( javaType );
if ( cached != null ) { if ( cached != null ) {

View File

@ -47,8 +47,6 @@ public class SchemaDropToOutputScriptTest {
private File output; private File output;
private ServiceRegistry serviceRegistry; private ServiceRegistry serviceRegistry;
private MetadataImplementor metadata; private MetadataImplementor metadata;
private final String dropMyEntityTable = "drop table if exists MyEntity";
private final String dropMySecondEntityTable = "drop table if exists MySecondEntity";
@BeforeEach @BeforeEach
@ -113,8 +111,8 @@ public class SchemaDropToOutputScriptTest {
); );
List<String> commands = Files.readAllLines( output.toPath() ); List<String> commands = Files.readAllLines( output.toPath() );
assertThat( commands.size(), is( 2 ) ); assertThat( commands.size(), is( 2 ) );
assertThat( commands.get( 0 ), containsString( dropMyEntityTable ) ); assertThat( commands.get( 0 ), containsString( getDropMyEntityTable() ) );
assertThat( commands.get( 1 ), containsString( dropMySecondEntityTable ) ); assertThat( commands.get( 1 ), containsString( getDropMySecondEntityTable() ) );
} }
@Test @Test
@ -131,8 +129,8 @@ public class SchemaDropToOutputScriptTest {
); );
List<String> commands = Files.readAllLines( output.toPath() ); List<String> commands = Files.readAllLines( output.toPath() );
assertThat( commands.size(), is( 11 ) ); assertThat( commands.size(), is( 11 ) );
assertThat( commands.get( 9 ), containsString( dropMyEntityTable ) ); assertThat( commands.get( 9 ), containsString( getDropMyEntityTable() ) );
assertThat( commands.get( 10 ), containsString( dropMySecondEntityTable ) ); assertThat( commands.get( 10 ), containsString( getDropMySecondEntityTable() ) );
} }
@Test @Test
@ -149,8 +147,26 @@ public class SchemaDropToOutputScriptTest {
); );
List<String> commands = Files.readAllLines( output.toPath() ); List<String> commands = Files.readAllLines( output.toPath() );
assertThat( commands.size(), is( 11 ) ); assertThat( commands.size(), is( 11 ) );
assertThat( commands.get( 9 ), containsString( dropMyEntityTable ) ); assertThat( commands.get( 9 ), containsString( getDropMyEntityTable() ) );
assertThat( commands.get( 10 ), containsString( dropMySecondEntityTable ) ); assertThat( commands.get( 10 ), containsString( getDropMySecondEntityTable() ) );
}
public String getDropMyEntityTable() {
if ( metadata.getDatabase().getDialect().supportsIfExistsBeforeTableName() ) {
return "drop table if exists MyEntity";
}
else {
return "drop table MyEntity if exists";
}
}
public String getDropMySecondEntityTable() {
if ( metadata.getDatabase().getDialect().supportsIfExistsBeforeTableName() ) {
return "drop table if exists MySecondEntity";
}
else {
return "drop table MySecondEntity if exists";
}
} }
@Entity(name = "MyEntity") @Entity(name = "MyEntity")