Spring Boot + Kotlin中的NamedNativeQuery / ConstructorResult:找不到PersistentEntity

时间:2018-09-06 18:33:03

标签: java spring-boot kotlin

我正在尝试使用NamedNativeQuery / ConstructorResult向AlertRepository添加查询方法。我们的Kotlin代码很大程度上基于以下Java示例:https://github.com/spring-projects/spring-data-examples/tree/master/jpa/jpa21#support-for-custom-sqlresultsetmapping-with-constructorresult

这是我添加到Alert实体中的内容:

@SqlResultSetMapping(name = "hourly-alert-counts",
        classes = [
            ConstructorResult(targetClass = HourlyAlertCount::class, columns = [
                ColumnResult(name = "day", type = Int::class),
                ColumnResult(name = "hour", type = Int::class),
                ColumnResult(name = "count", type = Long::class)
            ])
        ]
)
@NamedNativeQuery(
        name = "Alert.getHourlyCountsByConfigurationItemId",
        query = "select {fn DAYOFWEEK(reported_time)} as day, hour(reported_time) as hour, count(*) as count from alert\n" +
                "where configuration_item = ?1 group by configuration_item, {fn DAYOFWEEK(reported_time)}, hour(reported_time)",
        resultSetMapping = "hourly-alert-counts"
)
data class Alert(
  ...

并转到AlertRepository:

@Query(nativeQuery = true)
fun getHourlyCountsByConfigurationItemId(id: Long): List<HourlyAlertCount>

最后,HourlyAlertCount POJO:

data class HourlyAlertCount(
    val day: Int,
    val hour: Int,
    val count: Long
)

点击此端点时,出现以下错误:

{"cause":null,"message":"Couldn't find PersistentEntity for type class xxx.HourlyAlertCount!"}

ConstructorResult / ColumnResult应该是Java中的注释,但是如果我们在它们之前添加“ @”,则我们的Kotlin代码不会编译; this blog post建议忽略“ @”。我们正在使用spring-data-jpa 2.09和Hibernate 5.2.17。

0 个答案:

没有答案