6

Apparently with Hibernate 5, the geography type is supported. I've searched a lot, but found close to nothing related to the geography points.

What I want to do is the following:

  1. Save Long/Lat Points in the database table, e.g. ST_GeomFromText('POINT(6.463471 52.484832)', 4326)

  2. Perform queues to check if the Point is in a Long/Lat rectangle like :

    WHERE point && ST_MakeEnvelope (5.440433, 39.480434, 8.464680, 55.486190, 4326)

I'm using PostgreSQL and PostGIS.

I just cannot find which type of column annotation for hibernate I have to use to get Long/Lat Points, and also how to perform above query.

@Type(type="org.hibernate.spatial.GeometryType")

Is not a point regarding Long/Lat

Is there any documentation regarding this (note: not geometry points).

3 Answers 3

3

I'm using hibernate version 5.4.30

Regarding your question 1, You don't want to use any type of annotations to mark the field as Point type. Specifying the datatype as Point in your entity model is enough

@Entity
public class UserLocation {

    //other fields   
    private Point location;
    
    //constructors, getters & setters

Just make sure, you import Point from

import org.locationtech.jts.geom.Point;

To convert Latitude and longitude to Point, I use a Utility function

public class GeometryUtil {
    
    public static final int SRID = 4326; //LatLng
    private static WKTReader wktReader = new WKTReader();
    
    private static Geometry wktToGeometry(String wellKnownText) {
        Geometry geometry = null;
        
        try {
            geometry = wktReader.read(wellKnownText);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("###geometry :"+geometry);
        return geometry;
    }
     public static Point parseLocation(double x,double y) {
        Geometry geometry = GeometryUtil.wktToGeometry(String.format("POINT (%s %s)",x,y));
         Point p =(Point)geometry;
         p.setSRID(4326); 
         return p;
     }
}    

To create a point from latitude and longitude using

MySQL

Point p = GeometryUtil.parseLocation(latitude,longitude);

PostGIS

Point p = GeometryUtil.parseLocation(longitude,latitude);

2
  • 1
    Thank you, you saved my day. Yet, it should be reversed, double lng and double lat in parseLocation signature, otherwise it gets a bit messy when you use it.
    – woland
    Apr 30, 2021 at 16:56
  • Thanks, @woland, I'm very glad that my answer has helped you, I've written the code for MySQL. I have also added the code for PostGIS, could you please check it for me?
    – Saravanan
    May 2, 2021 at 14:37
1

Type will remain GeometryType just use distance in decimal and by below mention code you can get all location within 50 to 110 meters as i use 0.001, you can use according to your requirements.

GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326);
        factory.createPoint(new Coordinate(longitude,latitude));
        Point comparisonPoint = factory.createPoint(new Coordinate(longitude,latitude));
        predicates.add(SpatialPredicates.distanceWithin(cb, geom from db), comparisonPoint, 0.001));

distanceWithin() function take distance in degree which you have to cast to geograph but hibernate Spatial doesn't support so there is another way by which you can achieve this by providing distance in decimal according to your requirement.link is mention below for decimal points.

LINK : https://en.wikipedia.org/wiki/Decimal_degrees

-1

Yes, Hibernate 5 supports Spatial.

You need to add the dependecy

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
    <version>5.2.17.Final</version><!-- In my case was this version, needs to match with your hibernate core version-->
</dependency>

Then you need to use the right dialect depending on your PostgreSQL version.

A list of them:

org.hibernate.spatial.dialect.postgis.PostgisPG9Dialect
org.hibernate.spatial.dialect.postgis.PostgisPG82Dialect
org.hibernate.spatial.dialect.postgis.PostgisPG91Dialect
org.hibernate.spatial.dialect.postgis.PostgisPG92Dialect
org.hibernate.spatial.dialect.postgis.PostgisPG93Dialect
org.hibernate.spatial.dialect.postgis.PostgisPG94Dialect
org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect

After setting the correct dialect you can use Point or Polygon(there is no need to use @Type) in your entities. They come in the package com.vividsolutions.jts.geom

Creating a Point:

GeometryFactory gf = new GeometryFactory();
Point point = gf.createPoint(new Coordinate(lat, lon));
3
  • btw I did this using MySql. Sep 13, 2018 at 18:12
  • 5
    This answer misses the point of the question: the user is trying to persist a geography and not a geometry. Geographies can be persisted using Hibernate Spatial, but one should set the SRID of the geometry to 4326 (WGS84). Aug 8, 2019 at 12:14
  • Can anyone help stackoverflow.com/questions/76952490/…
    – Al Grant
    Aug 22 at 17:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.