在MySQL数据库中使用Hibernate Spatial的Spring Boot 问题 - 无法将值设置为点

Spring Boot with the usage of Hibernate Spatial in MySQL Database Issue - Cannot set the value to Point

提问人:S.N 提问时间:7/18/2023 最后编辑:S.N 更新时间:7/18/2023 访问量:167

问:

我在 Spring Boot 中使用 Hibanate Spatial 功能的使用者将纬度和经度设置为 Point 时遇到了问题。

我无法设置它,因为我想 .columnDefinition

我该如何解决?

下面是 Entity 类的相关部分,如下所示

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

下面是application.yml的数据库配置部分

spring:
  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect
        format_sql: true
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
java spring-boot hibernate-spatial

评论


答:

0赞 DASH 7/18/2023 #1

无需指定列定义。在实体模型中将数据类型指定为 Point 就足够了。

请使用点字段的标准设置器,并使用实用函数使用坐标计算点。

请参阅下面的答案 在 Spring Boot 中使用带有休眠空间 5 的 PostGIS 地理点

评论

0赞 S.N 7/18/2023
我想指定它的定义。定义就足够了吗?你能帮我解决这个问题吗?ST_GeomFromText(Point, 4326)
0赞 DASH 7/18/2023
指定定义是可以的。为了隔离问题,请先尝试不定义。同时将 point 字段的 setter 更改为 public void setPoint(Point point){ this.point=point}。
0赞 S.N 7/18/2023
@Column(name = "POINT", columnDefinition = "ST_GeomFromText(Point, 4326)") private Point point;这是对的吗?我为什么不在 Point 中使用。你能解释一下吗?geometry(Point,4326)
0赞 S.N 7/18/2023
哪一个是对的? 或geometry(Point,4326)ST_GeomFromText(Point, 4326)
0赞 DASH 7/18/2023
geometry(Point,4326) 不依赖于任何特定的 PostGIS 函数。所以我更愿意这样。首先尝试更换二传手。即 public void setPoint(double latitude, double longitude) {} 到 public void setPoint(Point point) {this.point=point} 并使用 util 方法计算点并传递给 setter。这应该有效。