0

Context: I've been trying to get Geometry Datatypes working with H2 before I eventually move to PostgreSQL. My code compiles but when I attempt to retrieve the User model it fails because of Geometry location. I get a value too long for column "LOCATION BINARY(255): 'X'aced005...."

application.properties

spring.datasource.url=jdbc:h2:file:./members.db
server.port = 8090

gradle.build, the main dependencies for this are the vividsolutions and some of the hibernate ones.

buildscript {
    ext {
        springBootVersion = '1.5.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()

}

dependencies {
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile (group: 'com.vividsolutions', name: 'jts', version: '1.13')
    compile (group: 'org.orbisgis', name: 'h2gis', version: '1.3.1')
    compile (group: 'org.hibernate', name: 'hibernate-core', version: '5.2.10.Final')
    compile (group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.10.Final')
    compile (group: 'org.hibernate', name: 'hibernate-spatial', version: '5.2.10.Final')
    runtime('com.h2database:h2')
    //  runtime('org.postgresql:postgresql')
}

Entity: If I don't provide getters/setters for location the application runs but that's because it's not actually trying to access that data. I've tried using other annotations to define Geometry for location, @Type(type="org.hibernate.spatial.GeometryType") Hibernate spatial docs. That annotation won't even compile because it doesn't recognize hibernate.spatial despite having the dependency in gradle.

package com.alex_donley.event_mapper.Entities;


import com.vividsolutions.jts.geom.Geometry;

import javax.persistence.*;

/**
 * Created by Indycorps on 5/11/2017.
 */
@Entity
public class User {

    @Id
    @GeneratedValue
    private long id;

    private String firstName;
    private String lastName;
    @Column(columnDefinition="Geometry")
    private Geometry location;

    public User(String firstName, String lastName, Geometry location) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.location = location;
    }

    public User(){}

    public long getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    //Create getters and setters for location to actually output stuff

    public Geometry getLocation() {
        return location;
    }

    public void setLocation(Geometry location) {
        this.location = location;
    }
}
Alex Donley
  • 145
  • 3
  • 16

2 Answers2

1

You're referring to outdated documentation. The correct documentation for your version is here.

Hibernate Spatial doesn't support H2, but it does support GeoDB which is based on H2. You might have more luck with that database.

Karel Maesen
  • 751
  • 5
  • 7
  • They are ways around that as shown in this post here: https://stackoverflow.com/questions/33607183/how-to-configure-spring-boot-project-to-work-with-inmemory-spatial-database-for – Timothy Mugayi Nov 04 '18 at 06:41
1

In my case, just changing the hibernate.dialect to org.hibernate.spatial.dialect.h2geodb.GeoDBDialectfix it. (according to v 5.2 hibernate docs here)

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219