spring-boothibernatejpaspring-data-jpahibernate-spatial

How to make spring boot, spatial hibernate and postgis work?


I can't start my spring boot(2.6.3) project with hibernate-spatial in create mode. It tells me that type "geometry does not exist". The geometry type comes from the hibernate-spatial library.

However, I applied everything necessary:

Here are my maven dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.6.3.Final</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.3.1</version>
    </dependency>
    <!--<dependency>
        <groupId>org.locationtech.jts</groupId>
        <artifactId>jts-core</artifactId>
        <version>1.18.2</version>
    </dependency>-->


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

My properties :

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/postgres?currentSchema=hibernatespatial
    username: postgres
    password: 
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect
    open-in-view: false
    database-platform: org.hibernate.spatial.dialect.postgis.PostgisDialect

My entity class :

package org.test.hibernate.spatial;


import org.geolatte.geom.Geometry;

import javax.persistence.*;

@Entity
@Table
public class Person {

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String lastname;

    private String age;

    private Geometry geom;

    public Geometry getGeom() {
        return geom;
    }

    public void setGeom(Geometry geom) {
        this.geom = geom;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

My repository class :

package org.test.hibernate.spatial;

import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepository extends JpaRepository<Person, Long> {


}

My boot class :

package org.test.hibernate.spatial;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@EnableJpaRepositories
@EnableTransactionManagement
public class TestHibernateSpatialApplication  implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(TestHibernateSpatialApplication.class, args);
    }


    public void run(String... args) throws Exception {

    }
}

My postgreSQL database is 14 version. Somebody have any idea what is wrong ?


Solution

  • PostGIS is a Postgres extension, which needs to be enabled for each database:

    Once PostGIS is installed, it needs to be enabled (Section 3.3, “Creating spatial databases”) or upgraded (Section 3.4, “Upgrading spatial databases”) in each individual database you want to use it in.
    [...]
    Run the following SQL snippet in the database you want to enable spatially:

    CREATE EXTENSION IF NOT EXISTS plpgsql;  
    CREATE EXTENSION postgis;
    

    Also be aware that the extension is by default installed to the default schema (e.g. public). So when using the currentSchema option, be sure to not accidentally exclude the schema postgis was installed into. To prevent this, one could either add the postgis schema to the currentSchema (e.g. jdbc:postgresql://localhost:5432/tst?currentSchema=app1,public), or move postgis to the preferred schema.