带变量的注解参数
Annotation argument with variable
在我们的 Spring 项目中,我想将 Java 存储库转换为 Kotlin。 Repository 是一个带有方法和注释的接口,其中包含数据库查询作为参数。有几种方法,但重复了查询中最复杂和最长的部分。在 Java 中,我们将一个字符串作为变量插入到查询中。
Kotlin 中的当前解决方案无法编译,因为 'an annotation argument must be a compile-time constant'。
处理此问题的最佳方法是什么?
interface PlaceRepository : JpaRepository<Place, UUID>, JpaSpecificationExecutor<Place> {
val HAVERSINE_FORMULA: String
get() =
"""
(6371 * acos(cos(radians(CAST(CAST(:latitude AS string) AS double)))
* cos(radians(p.gpsLatitude)) * cos(radians(p.gpsLongitude) - radians(CAST(CAST(:longitude AS string) AS double)))
+ sin(radians(CAST(CAST(:latitude AS string) AS double))) * sin(radians(p.gpsLatitude))))
"""
@Query("""
SELECT p AS place,
$HAVERSINE_FORMULA AS distance
FROM Place p
WHERE p.code = :code
""")
fun findByCode(
code: String,
latitude: Double,
longitude: Double
): Optional<PlaceWithDistanceDTO>
}
谢谢
您可以尝试如下使用伴生对象:
interface PlaceRepository : JpaRepository<Place, UUID>, JpaSpecificationExecutor<Place> {
companion object {
private const val HAVERSINE_FORMULA =
"""
(6371 * acos(cos(radians(CAST(CAST(:latitude AS string) AS double)))
* cos(radians(p.gpsLatitude)) * cos(radians(p.gpsLongitude) - radians(CAST(CAST(:longitude AS string) AS double)))
+ sin(radians(CAST(CAST(:latitude AS string) AS double))) * sin(radians(p.gpsLatitude))))
"""
}
@Query("""
SELECT p AS place,
$HAVERSINE_FORMULA AS distance
FROM Place p
WHERE p.code = :code
""")
fun findByCode(
code: String,
latitude: Double,
longitude: Double
): Optional<PlaceWithDistanceDTO>
}
在我们的 Spring 项目中,我想将 Java 存储库转换为 Kotlin。 Repository 是一个带有方法和注释的接口,其中包含数据库查询作为参数。有几种方法,但重复了查询中最复杂和最长的部分。在 Java 中,我们将一个字符串作为变量插入到查询中。
Kotlin 中的当前解决方案无法编译,因为 'an annotation argument must be a compile-time constant'。
处理此问题的最佳方法是什么?
interface PlaceRepository : JpaRepository<Place, UUID>, JpaSpecificationExecutor<Place> {
val HAVERSINE_FORMULA: String
get() =
"""
(6371 * acos(cos(radians(CAST(CAST(:latitude AS string) AS double)))
* cos(radians(p.gpsLatitude)) * cos(radians(p.gpsLongitude) - radians(CAST(CAST(:longitude AS string) AS double)))
+ sin(radians(CAST(CAST(:latitude AS string) AS double))) * sin(radians(p.gpsLatitude))))
"""
@Query("""
SELECT p AS place,
$HAVERSINE_FORMULA AS distance
FROM Place p
WHERE p.code = :code
""")
fun findByCode(
code: String,
latitude: Double,
longitude: Double
): Optional<PlaceWithDistanceDTO>
}
谢谢
您可以尝试如下使用伴生对象:
interface PlaceRepository : JpaRepository<Place, UUID>, JpaSpecificationExecutor<Place> {
companion object {
private const val HAVERSINE_FORMULA =
"""
(6371 * acos(cos(radians(CAST(CAST(:latitude AS string) AS double)))
* cos(radians(p.gpsLatitude)) * cos(radians(p.gpsLongitude) - radians(CAST(CAST(:longitude AS string) AS double)))
+ sin(radians(CAST(CAST(:latitude AS string) AS double))) * sin(radians(p.gpsLatitude))))
"""
}
@Query("""
SELECT p AS place,
$HAVERSINE_FORMULA AS distance
FROM Place p
WHERE p.code = :code
""")
fun findByCode(
code: String,
latitude: Double,
longitude: Double
): Optional<PlaceWithDistanceDTO>
}