hibernate 如何设置Quarkus与Postgis一起工作?

ryhaxcpt  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(137)

我试着用Postgis运行一个简单的例子,但我得到了错误:
第一个月
我知道开发人员服务升级到PostgreSQL 14.8。
我的pom.xml有这些依赖项:

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

字符串
在我的application.properties中,我有以下属性:
quarkus.hibernate-orm.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
我该如何解决这个问题?
虽然我无法在本地解决它(quarkus dev services + Postgis),但我正在尝试以下解决方案:
下载Postgis docker镜像(https://hub.docker.com/r/postgis/postgis),创建数据库(test),配置如下:

%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

quarkus.hibernate-orm.dialect=org.hibernate.dialect.PostgreSQLDialect


我的类类似于下面的代码:

@Entity
public class Route implements Serializable {

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

    @Column(nullable = false)
    private String description;

    @JsonIgnore
    @Column(columnDefinition = "Geometry")
    private PGgeometry coordinates;
    ...
   }


我正在尝试发送此有效载荷:

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


然后,我创建了一个RouteDTO:

public class RouteDTO {
    public String description;
    public double[][] coordinates;
}


我的RouteResource是:

@Path("/routes")
public class RouteResource {

    @Inject
    RouteRepository repository;

    private static PGgeometry 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]);
        }
        LinearRing outerRing = new LinearRing(points);
        return new PGgeometry(outerRing);
    }

    @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();
    }
}


当我尝试调用POST时,我得到了错误:
Caused by: org.postgresql.util.PSQLException: ERROR: Invalid endian flag value encountered.
镜像docker安装了PostgreSQL 15.3和Postgis 3.3.4
有没有人知道如何解决这个问题?
还尝试:

  • Docker镜像postgis/postgis:14-3.3,安装PostgreSQL 14.8和Postgis 3.3.4,但我得到了相同的endian错误。
  • Docker镜像postgis/postgis:9.6-3.2,安装PostgreSQL 9.6.24和PostGis 3.2.0,但得到错误:原因:java.lang.NoSuchMethodError:'void org. hibernate. dialect. PostgreSQL 81Dialect.registerColumnType(int,java.lang.String)'
wgx48brx

wgx48brx1#

我通过将获取Geometry对象的包从net.postgis.jdbc.geometry.* 更改为org.locationtech.jts.geom.* 并在代码中进行小调整来解决这个问题:

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);
}

字符串

相关问题