Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

H2GIS data in Springboot 2.6

I am trying to persist GIS point data in spring-boot 2.6 with a H2 in mem database.

I have followed the setup as per here with the exception of the newer spring-boot version.

My build.gradle

plugins {
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'com.nz'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('azureVersion', "3.14.0")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.azure.spring:azure-spring-boot-starter-active-directory'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation group: 'com.h2database', name: 'h2', version: '1.4.197'
    //implementation group: 'org.locationtech.jts', name: 'jts-core', version: '1.18.2'
    implementation group: 'com.graphhopper.external', name: 'jackson-datatype-jts', version: '1.0-2.7'
    implementation group: 'org.hibernate', name: 'hibernate-spatial', version: '6.0.0.CR2', ext: 'pom'
    implementation group: 'org.orbisgis', name: 'h2gis', version: '1.5.0'
}

my application.properties

spring.jpa.hibernate.ddl-auto=update
spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

# Initialise H2 with H2GIS for spatial support – see schema-h2.sql also
spring.datasource.platform=h2
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

My schema.sql

-- Needed to add H2GIS support for spatial data types
CREATE ALIAS IF NOT EXISTS H2GIS_SPATIAL FOR "org.h2gis.functions.factory.H2GISFunctions.load";
CALL H2GIS_SPATIAL();

Abbreviated entity

@Id
@GeneratedValue(strategy = AUTO)
private Long id;
@Column(columnDefinition = "POINT")
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(using = GeometryDeserializer.class)
private Point location;

But I get the following error when I try to run:

org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.spatial.dialect.h2geodb.GeoDBDialect] as strategy [org.hibernate.dialect.Dialect]
like image 252
Al Grant Avatar asked Feb 04 '26 11:02

Al Grant


1 Answers

As posted in the question, only version 1.4.197 of H2 works with H2GIS. On order to get this working with SpringBoot 2.6, which uses much newer versions of H2 I had to override H2 version in my build.gradle.

BUILD.GRADLE

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

ext['h2.version'] = '1.4.197'

group = 'com.nz'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
...

Note the ext[h2.version] = '1.4.197' this overrides the Springboot 2.6 default h2 version.

I also used a slightly older version of h2gis in my build.gradle

implementation group: 'org.orbisgis', name: 'h2gis', version: '1.5.0'

This was again for compatability reasons.

like image 126
Al Grant Avatar answered Feb 06 '26 02:02

Al Grant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!