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

View File

@ -13,7 +13,7 @@ ext {
junitVintageVersion = '5.7.1'
junit5Version = '5.7.1'
h2Version = '1.4.200'
h2Version = '1.4.197'
bytemanVersion = '4.0.16' //Compatible with JDK16
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
getDefaultProperties().setProperty( AvailableSettings.NON_CONTEXTUAL_LOB_CREATION, "true" );
registerColumnType( SqlTypes.ARRAY, "array" );
if ( version >= 104032 ) {
this.sequenceInformationExtractor = version >= 104201
? SequenceInformationExtractorLegacyImpl.INSTANCE
: SequenceInformationExtractorH2DatabaseImpl.INSTANCE;
this.querySequenceString = "select * from INFORMATION_SCHEMA.SEQUENCES";
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 {
this.sequenceInformationExtractor = SequenceInformationExtractorNoOpImpl.INSTANCE;
this.querySequenceString = null;
}
if ( version < 200 ) {
// prior to version 2.0, H2 reported NUMERIC columns as DECIMAL,
// which caused problems for schema update tool
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
@ -156,9 +158,14 @@ public class H2Dialect extends Dialect {
final JdbcTypeRegistry jdbcTypeRegistry = typeContributions.getTypeConfiguration()
.getJdbcTypeDescriptorRegistry();
if ( version >= 104197 ) {
jdbcTypeRegistry.addDescriptorIfAbsent( UUIDJdbcType.INSTANCE );
if ( version >= 104198 ) {
jdbcTypeRegistry.addDescriptorIfAbsent( DurationIntervalSecondJdbcType.INSTANCE );
}
}
}
private static int parseBuildId(DialectResolutionInfo info) {
final String databaseVersion = info.getDatabaseVersion();
@ -205,7 +212,9 @@ public class H2Dialect extends Dialect {
CommonFunctionFactory.dayOfWeekMonthYear( queryEngine );
CommonFunctionFactory.weekQuarter( queryEngine );
CommonFunctionFactory.daynameMonthname( queryEngine );
if ( useLocalTime ) {
CommonFunctionFactory.localtimeLocaltimestamp( queryEngine );
}
CommonFunctionFactory.bitLength( queryEngine );
CommonFunctionFactory.octetLength( queryEngine );
CommonFunctionFactory.ascii( queryEngine );

View File

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

View File

@ -112,6 +112,10 @@ public class JavaTypeRegistry implements JavaTypeDescriptorBaseline.BaselineTarg
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) {
final JavaType<?> cached = descriptorsByType.get( javaType );
if ( cached != null ) {

View File

@ -47,8 +47,6 @@ public class SchemaDropToOutputScriptTest {
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";
@BeforeEach
@ -113,8 +111,8 @@ public class SchemaDropToOutputScriptTest {
);
List<String> commands = Files.readAllLines( output.toPath() );
assertThat( commands.size(), is( 2 ) );
assertThat( commands.get( 0 ), containsString( dropMyEntityTable ) );
assertThat( commands.get( 1 ), containsString( dropMySecondEntityTable ) );
assertThat( commands.get( 0 ), containsString( getDropMyEntityTable() ) );
assertThat( commands.get( 1 ), containsString( getDropMySecondEntityTable() ) );
}
@Test
@ -131,8 +129,8 @@ public class SchemaDropToOutputScriptTest {
);
List<String> commands = Files.readAllLines( output.toPath() );
assertThat( commands.size(), is( 11 ) );
assertThat( commands.get( 9 ), containsString( dropMyEntityTable ) );
assertThat( commands.get( 10 ), containsString( dropMySecondEntityTable ) );
assertThat( commands.get( 9 ), containsString( getDropMyEntityTable() ) );
assertThat( commands.get( 10 ), containsString( getDropMySecondEntityTable() ) );
}
@Test
@ -149,8 +147,26 @@ public class SchemaDropToOutputScriptTest {
);
List<String> commands = Files.readAllLines( output.toPath() );
assertThat( commands.size(), is( 11 ) );
assertThat( commands.get( 9 ), containsString( dropMyEntityTable ) );
assertThat( commands.get( 10 ), containsString( dropMySecondEntityTable ) );
assertThat( commands.get( 9 ), containsString( getDropMyEntityTable() ) );
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")