在hibernate注解中设置srid

nvbavucw  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(316)

我使用springboot2.1和hibernate以及mysql 8开发gis项目。
我有一张有这个细节的table

@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属性添加到带注解的点,以及如何将空间索引添加到带注解的点。

bjp0bcyl

bjp0bcyl1#

尝试使用列定义。使用Hibernate5.4.2和MySQL8.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

相关问题