2

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:

  • add hibernate-spatial dependency (my version 5.6.3.Final)

  • use the org.hibernate.spatial.dialect.postgis.PostgisDialect dialect Moreover this class is deprecated and the documentation corresponding to the same version, it still indicates to use it, I do not understand anything (https://docs.jboss.org/hibernate/orm/5.6/userguide /html_single/Hibernate_User_Guide.html#spatial)

  • Use Geometry type from geolatte group or jts group Despite that I have an error where it cannot create the table because the type "geometry does not exist".

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 ?

Fanga
  • 67
  • 1
  • 7

1 Answers1

1

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.

fladdimir
  • 1,230
  • 1
  • 5
  • 12
  • 1
    I created another database, activated the extension with the commands you gave me and it worked. In the database before, they were however activated but it does not work. I'm using a jdbc connection with the "currentSchema" option. With this option it does not detect the postgis extension. Would you have any idea why? – Fanga Feb 06 '22 at 21:00
  • https://stackoverflow.com/questions/39460459/search-path-doesnt-work-as-expected-with-currentschema-in-url -> looks like the postgis extension was installed to a different schema, and `currentSchema` sets the complete search-path – fladdimir Feb 07 '22 at 01:15
  • 1
    I used hibernate's default schema property instead of currentSchema on the jdbc string. Thank you very much ! I thought I had version incompatibilities or a misunderstanding about this library. – Fanga Feb 07 '22 at 01:23