简体   繁体   English

如何使用spring boot JPA在Postgres中存储Geometry Point?

[英]How to store Geometry Point in Postgres using spring boot JPA?

To store longitude and latitude as geometry location as Point in Postgres using spring boot jpa. 使用弹簧靴jpa将经度和纬度作为几何位置存储为Postgres中的Point。

After applying below code it throws: column "location" is of type point but expression is of type bytea. 在应用下面的代码后,它会抛出:列“location”的类型为point,但表达式的类型为bytea。

Also when fetching data it throws: could not deserialize; 此外,在获取数据时,它会抛出:无法反序列化; nested exception is org.hibernate.type.SerializationException: could not deserialize 嵌套异常是org.hibernate.type.SerializationException:无法反序列化

Add dependency in pom.xml 在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>

Add column in entity class. 在实体类中添加列。

@Column(columnDefinition = "POINT")
private Point location;

For storing data into database 用于将数据存储到数据库中

GeometryFactory geometryFactory = new GeometryFactory();

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

            Point myPoint = geometryFactory.createPoint(coordinate);
            user.setLocation(myPoint);

I need to store data as (30.5,53.123) format in Postgres. 我需要在Postgres中将数据存储为(30.5,53.123)格式。

To add an extension of postgis in Postgresql. 在Postgresql中添加postgis的扩展名。 Add extension in particular schema as per below query. 根据以下查询在特定架构中添加扩展。

CREATE EXTENSION postgis;

And change hibernate dialect with spatial.dialect.postgis.PostgisDialect 并使用spatial.dialect.postgis.PostgisDialect更改hibernate方言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM