This question applies to a Java Spring Boot app using Hibernate to persist data to Postgres with PostGIS installed.
I’m running Hibernate 6.4.4.Final and read that Hibernate spatial may need to be the exact same version, so in my pom.xml (below) I’ve specified 6.4.4.Final for it as well.
Java 17
Spring Boot 3.2.4
PostgreSQL 16.3
Hibernate dialect: org.hibernate.dialect.PostgreSQLDialect
I have an entity class where every field is getting persisted correctly during save EXCEPT for the coordinates field with type org.locationtech.jts.geom.Polygon. It’s value gets built by a mapper that converts a List<List> into a Polygon.
entity class snippet:
import org.locationtech.jts.geom.Polygon;
@Column(name = "coordinates")
private Polygon coordinates;
Mapper class:
@Mapping(
target = "coordinates",
source = "coordinates",
qualifiedByName = "toPolygon"
)
@Named("toPolygon")
default Polygon toPolygon(List<List<BigDecimal>> inCoordinates) {
Coordinate[] coordinates = {
new Coordinate(inCoordinates.get(0).get(1).doubleValue(), inCoordinates.get(0).get(0).doubleValue()),
new Coordinate(inCoordinates.get(1).get(1).doubleValue(), inCoordinates.get(1).get(0).doubleValue()),
new Coordinate(inCoordinates.get(2).get(1).doubleValue(), inCoordinates.get(2).get(0).doubleValue()),
new Coordinate(inCoordinates.get(3).get(1).doubleValue(), inCoordinates.get(3).get(0).doubleValue())
};
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
org.locationtech.jts.geom.Polygon polygon = new org.locationtech.jts.geom.Polygon(linearRing, null, geometryFactory);
return polygon;
}
When I attempt to save the entity via a JpaRepository class .save() method I get this error:
“Unable to bind parameter #3 – POLYGON ((90 90, 90 90, 90 90, 90 90)) [Unknown type geometry.] [n/a]; SQL [n/a]”
Most of the solutions I’ve found on Stack Overflow worked for previous versions of Hibernate Spatial when it was necessary to add a specific @Type annotation to the entity field, but apparently that’s no longer the case in newer versions? I’ve experimented with defining the coordinates field as Geometry instead of Polygon but the error continues either way.
Am I building the Polygon correctly? Is there another annotation I need in the entity class?
pom.xml dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-common</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>9.0.86</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.12.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>6.4.4.Final</version>
</dependency>
</dependencies>