在休眠注释中设置srid

时间:2018-12-04 05:48:25

标签: mysql hibernate spring-boot

我使用Spring Boot 2.1以及Hibernate和mysql 8来处理gis项目。

我有一张详细的表格

@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Place {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long Id;

    @Column(nullable = false, columnDefinition = "point")
    Point point;

    String tittle;
    String text;

    @Column(name = "created_date", nullable = false, updatable = false)
    @CreatedDate
    @JsonIgnore
    private long createdDate;

    @Column(name = "modified_date")
    @LastModifiedDate
    @JsonIgnore
    private long modifiedDate;

    @org.springframework.data.annotation.CreatedBy
    @JsonIgnore
    Long CreatedBy;

    @LastModifiedBy
    @JsonIgnore
    Long ModifiedBy;

    @ManyToOne(fetch=FetchType.LAZY,optional = false)
    @JoinColumn(name="city_id",nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    private City city;
}

我想为点添加索引,但是mysql让我警告:

查询优化器将不使用列'point'上的空间索引,因为该列没有SRID属性。考虑将SRID属性添加到列中。

如何将srid属性添加到带有注释的点,以及如何添加空间索引到带有注释的点。

1 个答案:

答案 0 :(得分:1)

尝试使用列定义。经过Hibernate 5.4.2和MySQL 8.0的测试。

@Entity
public class GeometryEntity {

  @Id
  @GeneratedValue
  private Long id;

  // only allow geometries with SRID 4326 and not null (be able to create spatial indexes)
  @Column(nullable = false, columnDefinition = "GEOMETRY SRID 4326")
  private Geometry geometry;

  private void setId(final Long id) {
    this.id = id;
  }

  public Long getId() {
    return id;
  }

  public void setGeometry(final Geometry geometry) {
    this.geometry = geometry;
  }

  public Geometry getGeometry() {
    return geometry;
  }

}

另请参阅: https://dev.mysql.com/doc/refman/8.0/en/spatial-type-overview.html