如何使用spring boot JPA在Postgres中存储几何点?

Pri*_*lal 6 postgresql spring-data-jpa spring-boot

使用 spring boot jpa 将经度和纬度作为几何位置存储为 Postgres 中的 Point。

应用下面的代码后,它会抛出:列“位置”的类型是点,但表达式的类型是 bytea。

同样在获取数据时它抛出:无法反序列化;嵌套异常是 org.hibernate.type.SerializationException:无法反序列化

在 pom.xml 中添加依赖

<dependency>
    <groupId>com.vividsolutions</groupId>
    <artifactId>jts</artifactId>
    <version>1.13</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
    <version>5.4.1.Final</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在实体类中添加列。

@Column(columnDefinition = "POINT")
private Point location;
Run Code Online (Sandbox Code Playgroud)

用于将数据存储到数据库中

GeometryFactory geometryFactory = new GeometryFactory();

            Coordinate coordinate = new Coordinate();
            coordinate.x = 2;
            coordinate.y = 5;

            Point myPoint = geometryFactory.createPoint(coordinate);
            user.setLocation(myPoint);
Run Code Online (Sandbox Code Playgroud)

我需要在 Postgres 中将数据存储为 (30.5,53.123) 格式。

Pri*_*lal 0

在Postgresql中添加postgis的扩展。按照以下查询在特定架构中添加扩展。

CREATE EXTENSION postgis;
Run Code Online (Sandbox Code Playgroud)

并使用 Spatial.dialect.postgis.PostgisDialect 更改休眠方言