0

I have created a table in postgresql as:

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

I have to read the data using spring's reactive support for relational databases(r2dbc). I am not sure how to map geom column to a field in java class.

My incomplete class is:

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

What java datatype would map to postgresql geometry? How to do it?

Edit: spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect

Error is in code:

FeatureRepository featureRepository = applicationContext.getBean(FeatureRepository.class);
        featureRepository.findAll().doOnNext(feature -> {
            log.info(feature.toString());
        }).blockLast(Duration.ofSeconds(10));
3
  • 1
    Hey there. In which format do you want to have your geometry? WKT, GeoJSON, KML, etc.. Commented Jun 9, 2020 at 10:21
  • I am looking for GeoJSON. Commented Jun 9, 2020 at 10:24
  • I'm not very familiar with spring-boot, but if you can pass a function to the column geom you could try ST_AsGeoJSON(geom). I've also seen this syntax before: @Column(name = "geom", columnDefinition = "geometry(Point,4326)") Commented Jun 9, 2020 at 10:27

1 Answer 1

1

Use com.vividsolutions.jts.geom.

Here I am giving an example of Point

@Column("geom")
private Point location;

And you can create Point using GeometryFactory

GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(new Coordinate(12.34343, 12.232424));

Add hibernate-spatial in pom.xml

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

Then you need to add dialect depending on your PostgreSQL

org.hibernate.spatial.dialect.postgis.PostgisPG9Dialect
Sign up to request clarification or add additional context in comments.

13 Comments

I have field as Geometry, and it gives error: Caused by: java.lang.IllegalStateException: Required identifier property not found for class com.vividsolutions.jts.geom.Geometry!
@Column("geom") @Type(type="org.hibernate.spatial.GeometryType") public com.vividsolutions.jts.geom.Geometry geom; spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
What is your PostgreSQL version ? When you are getting error saving ?
Its Postgresql 12, and I get error when I read records. Records are already there in the database table. I am using spring boot r2dbc support.
yup, raised. Thanks for all the inputs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.