0

I have a problem to set latitude and longitude to Point with the usager of Hibanate Spatial feature in Spring Boot.

I cannot set it as I guess there is a problem in columnDefinition.

How can I fix it?

Here is the relevant part of the Entity class shown below

@Column(name = "POINT", columnDefinition = "geometry(Point,4326)")
private Point point;

public void setPoint(double latitude, double longitude) {
    Coordinate coordinate = new Coordinate(latitude, longitude);
    GeometryFactory geometryFactory = new GeometryFactory();
    this.point = geometryFactory.createPoint(coordinate);
}

Here is the database configuration part of application.yml

spring:
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect
        format_sql: true
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
S.N
  • 2,157
  • 3
  • 29
  • 78

1 Answers1

0

You don't need to specify column definition.Specifying the datatype as Point in your entity model is enough.

Please use the standard setter for the point field and use a utility function to calculate point using the coordinates.

Refer the answer below Use PostGIS geography point with hibernate spatial 5 in spring boot

DASH
  • 124
  • 5
  • I want to specify its defination. Is `ST_GeomFromText(Point, 4326)` enough for definition? Can you help me solve the issue. – S.N Jul 17 '23 at 20:15
  • Specifying the definition is fine. In order to isolate the issue, try with no definition first. Also change setter for the point field to public void setPoint(Point point){ this.point=point}. – DASH Jul 17 '23 at 20:19
  • `@Column(name = "POINT", columnDefinition = "ST_GeomFromText(Point, 4326)") private Point point;` Is it right? Why don't I use `geometry(Point,4326)` in the Point. Can you explain it? – S.N Jul 17 '23 at 21:37
  • Which one is right? `geometry(Point,4326)` or `ST_GeomFromText(Point, 4326)` – S.N Jul 18 '23 at 10:18
  • geometry(Point,4326) does not rely on any specific PostGIS functions. So I would prefer that. Try changing the setter first. i.e public void setPoint(double latitude, double longitude) {} to public void setPoint(Point point) {this.point=point} and use a util method to calculate point and pass to setter. This should work. – DASH Jul 18 '23 at 10:36
  • I have to define `double latitude, double longitude` as `parameters` in the `setPoint` method. My issue is different. `geometry(Point,4326)` or `ST_GeomFromText(Point, 4326)` Which one is right? – S.N Jul 18 '23 at 12:27
  • The first is right: geometry(POINT, 4326) – Karel Maesen Jul 19 '23 at 09:23
  • @Dash similar Q with no answer : https://stackoverflow.com/questions/76952490/point-data-type-into-mysql8 – Al Grant Aug 22 '23 at 17:57