【问题标题】:Spring Data JPA + KotlinSpring Data JPA + Kotlin
【发布时间】:2020-01-27 17:21:16
【问题描述】:

在 Spring Data JPA 中尝试使用接口投影技术:

interface DocInfoShort {
    var docTypeId: Int
}

还有我的 DAO:

public interface DocRepository extends JpaRepository<Doc, Long> {
    @Query("select sum(cast(doc.isSent AS int)) as docTypeId" +
            "       from Doc doc group by docTypeId", nativeQuery = true)
    fun someQuery(): List<DocInfoShort>
}

但它不起作用:

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"

我该如何解决它。

【问题讨论】:

    标签: spring hibernate kotlin spring-data-jpa hql


    【解决方案1】:

    只需将DocInfoShort 设为具有适当构造函数的类,并在查询注释中使用 JPQL 的构造函数语法即可:

    @Query("select new full.package.name.DocInfoShort(sum(cast(doc.isSent AS int)))" +
           "from Doc doc group by docTypeId")
    

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2023-03-13
      • 2020-03-01
      • 2017-07-17
      • 1970-01-01
      • 2017-11-29
      • 2021-09-11
      • 2018-04-18
      • 2020-06-16
      相关资源
      最近更新 更多