将postgresql几何列Map到java对象中的字段

66bbxpm5  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(239)

我在postgresql中创建了一个表:

CREATE TABLE feature (ID serial, feature_name text, geom geometry(GEOMETRYZ,4326));

我必须使用spring对关系数据库(r2dbc)的React式支持来读取数据。我不知道如何将geom列Map到java类中的字段。
我不完整的课程是:

import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.annotation.Id;

@Table
public class Feature {

    @Id
    private Long id;

    @Column("feature_name")
    private String featureName;

    @Column("geom")
    @Type(type="org.hibernate.spatial.GeometryType")
    public com.vividsolutions.jts.geom.Geometry geom;
}

什么java数据类型会Map到postgresql几何体?怎么做?
编辑:spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.postgisdialect
代码中有错误:

FeatureRepository featureRepository = applicationContext.getBean(FeatureRepository.class);
        featureRepository.findAll().doOnNext(feature -> {
            log.info(feature.toString());
        }).blockLast(Duration.ofSeconds(10));
nmpmafwu

nmpmafwu1#

使用com.solutions.jts.geom。
这里我举一个例子 Point ```
@Column("geom")
private Point location;

你可以创造 `Point` 使用 `GeometryFactory` ```
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(new Coordinate(12.34343, 12.232424));

添加 hibernate-spatial 在pom.xml中

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
    </dependency>

然后您需要根据您的postgresql添加方言

org.hibernate.spatial.dialect.postgis.PostgisPG9Dialect

相关问题