8

To store longitude and latitude as geometry location as Point in Postgres using spring boot jpa.

After applying below code it throws: column "location" is of type point but expression is of type bytea.

Also when fetching data it throws: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize

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

Priya Dalal
  • 91
  • 1
  • 6

2 Answers2

0

To add an extension of postgis in Postgresql. Add extension in particular schema as per below query.

CREATE EXTENSION postgis;

And change hibernate dialect with spatial.dialect.postgis.PostgisDialect

ndsmyter
  • 6,535
  • 3
  • 22
  • 37
Priya Dalal
  • 91
  • 1
  • 6
0

I found this Stack overflow post, you can use point data type to store latitude and longitude values. This data type combines (x,y) which can be latitude, longitude. If you want more precision, you can add two columns of type float or double precision.

You can also use PostGIS, which gives you addition data types geometry and geography. These data types occupy one column of 32 bytes for a point. You can refer to PostGIS manual