Quarkus + Hibernate + Postgis => Invalid endian flag value encountered

Hi all!

I’m trying to run a simple Quarkus project with Postgis, but when I persist an entity with a property of type Geometry I get the error below:

Caused by: org.postgresql.util.PSQLException: ERROR: Invalid endian flag value encountered.

Initially I ran a docker container with the following command:

docker run --name postgis -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgis/postgis

So I created a database called test using template_postgis.

I’m using the following dependencies:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
    <version>6.2.7.Final</version>
</dependency>
<dependency>
    <groupId>net.postgis</groupId>
    <artifactId>postgis-jdbc</artifactId>
    <version>2021.1.0</version>
</dependency>

The application.properties file looks like this:

%dev.quarkus.datasource.db-kind=postgresql 
%dev.quarkus.datasource.username=postgres
%dev.quarkus.datasource.password=mysecretpassword
%dev.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/test
%dev.quarkus.hibernate-orm.database.generation=drop-and-create
%dev.quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQLDialect

Here you can see my entity:

import jakarta.persistence.*;
import net.postgis.jdbc.geometry.Geometry;

import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;

@Entity
public class Route implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    @Column(nullable = false)
    private String description;

    @Column(columnDefinition = "Geometry")
    private Geometry coordinates;
    // getters and setters
}

Below you can see my DTO:

public class RouteDTO {

    public String description;
    public double[][] coordinates;

}

that is used in my resource (this way was the last attempt among countless I’ve tried):

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import net.postgis.jdbc.geometry.Geometry;
import net.postgis.jdbc.geometry.MultiPoint;
import net.postgis.jdbc.geometry.Point;

import java.net.URI;
import java.util.List;

@Path("/routes")

public class RouteResource {

    @Inject
    RouteRepository repository;

    private static Geometry createPGGeometry(double[][] coordinates) {
        Point[] points = new Point[coordinates.length];
        for (int i = 0; i < coordinates.length; i++) {
            if (coordinates[i].length != 2) {
                throw new IllegalArgumentException("Invalid coordinate format: " + coordinates[i]);
            }
            points[i] = new Point(coordinates[i][0], coordinates[i][1]);
        }

        return new MultiPoint(points);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Route> list() {
        return repository.listAll();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response create(RouteDTO routeDTO) {
        Route route = new Route(routeDTO.description, createPGGeometry(routeDTO.coordinates));
        repository.persist(route);
        return Response.created(URI.create("/routes/" + route.getId())).build();
    }
}

My repository is quite simple:

@ApplicationScoped
@Transactional
public class RouteRepository implements PanacheRepository<Route> { }

Here is the payload sent in the POST request:

{
    "description": "Rota 66",
    "coordinates": [
    [0.0, 0.0],
    [1.0, 0.0],
    [1.0, 1.0],
    [0.0, 1.0],
    [0.0, 0.0]
  ]
}

Any idea how to resolve this?

I solved the problem by changing the package where I get the Geometry objects from net.postgis.jdbc.geometry.* to org.locationtech.jts.geom.* and small adustments in the code:

  private static Geometry createPGGeometry(double[][] coordinates) {
    
        GeometryFactory factory = new GeometryFactory();
    
        Point[] points = new Point[coordinates.length];
        for (int i = 0; i < coordinates.length; i++) {
            if (coordinates[i].length != 2) {
                throw new IllegalArgumentException("Invalid coordinate format: " + coordinates[i]);
            }
            points[i] = factory.createPoint(new Coordinate(coordinates[i][1], coordinates[i][0]));
        }
    
        return factory.createMultiPoint(points);
    }

See Hibernate ORM 6.2.7.Final User Guide for information about spatial in Hibernate.