H2GIS data in Springboot 2.6
Asked Answered
V

1

3

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]
Vladikavkaz answered 31/3, 2022 at 11:16 Comment(0)
V
1

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.

Vladikavkaz answered 3/4, 2022 at 1:56 Comment(2)
I tried applying your solution to my maven setup, where I want to use h2 only for my integration tests, but I'm still getting Could not obtain connection to query metadata org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [ org.hibernate.spatial.dialect.h2geodb.GeoDBDialect] as strategy [org.hibernate.dialect.Dialect]Randellrandene
-> Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.spatial.dialect.h2geodb.GeoDBDialect. GeoDBDialect is included in hibernate-spatial:5.6.7 ...Randellrandene

© 2022 - 2024 — McMap. All rights reserved.