Spring Boot JPA-查找在给定位置范围(lat,lng)和半径(距离)内的位置

时间:2020-02-22 18:12:04

标签: spring spring-boot spring-data-jpa

我试图在Spring JPA中编写一个mysql查询,以查找给定范围内的位置。 (我正在使用Spring Boot + Kotlin)

我正在尝试以下查询:

@Repository
interface LocationRepository : JpaRepository<LocationEntity, Long> {

    @Query("SELECT id, (6371 * acos ( cos ( radians(:lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(:lng) ) + sin ( radians(:lat) ) * sin( radians( lat ) ))) AS distance FROM location GROUP BY address HAVING distance < :distance ORDER BY distance ASC", nativeQuery=true)
    fun filterByLocation(
        @Param("lat") lat: String,
        @Param("lng") lng: String,
        @Param("distance") distance: Int
    ): List<LocationEntity>
}

但是出现以下错误:

java.sql.SQLSyntaxErrorException: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'my_app.location.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

我的位置实体如下:

@Entity(name = "location")
data class LocationEntity(
    var lat: Double,
    var lng: Double
) {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = 0
}

0 个答案:

没有答案
相关问题