【问题标题】:Kotlin JPA query inner join using two typesKotlin JPA 查询内连接使用两种类型
【发布时间】:2019-12-31 19:10:40
【问题描述】:

我是 Kotlin 和 JPA 的新手。我有一个从两个表(Postgres)获取数据的内部连接查询。查询工作正常。但是,由于我现在有两种类型(两张表),因此使用其中一种只会返回其中一张表中的所有字段。为了返回所有字段,我将类型更改为 List。但是,当我这样做时,我返回的对象没有字段,只有原始数据。如何更改我的代码,以便我的 json 响应包含字段名称和数据。

对不起,如果我的问题不清楚,我对 Kotlin 很陌生。

更新的代码 我的存储库代码

package com.sg.xxx.XXXTTT.report.repository
import com.sg.xxx.XXXTTT.report.model.Report
import com.sg.xxx.XXXTTT.report.model.ReportWithBatches
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
import java.time.LocalDate


@Repository
interface IReportRepository : JpaRepository<Report, Long> {

    fun findAllByCreationDate(date: LocalDate): List<Report>

    fun findByReportName(name: String): Report?

    fun findByAdlsFullPath(name: String): Report?

    @Query("SELECT new com.sg.xxx.xxxttt.report.model.ReportWithBatches(r.adlsFullPath, r.sentToXXX, r.contentLength, r.creationDate, r.remoteFileNameOnFTA, b.dataPath , b.version, b.source, r.numberOfRecords) FROM Report r INNER JOIN BatchInfo b ON r.reportUuid = b.reportUuid WHERE r.creationDate = ?1")
    fun findAllByCreationDateJoinBatches(date: LocalDate): List<ReportWithBatches>
}



我的控制器代码

    @GetMapping(value = ["/linkBatches/{today}"])
    fun findAllByCreationDateJoinBatches(@PathVariable("today") @DateTimeFormat(pattern = "yyyyMMdd") date: LocalDate): List<ReportWithBatches> {
        return eligibleService.findAllByCreationDateJoinBatches(date)
    }

我的 DTO

package com.sg.xxx.xxxttt.report.model
import java.time.LocalDate

open class ReportWithBatches(
        var adlsFullPath: String?,
        var sentToXXX: Boolean?,
        var contentLength: Long?,
        var creationDate: LocalDate,
        var remoteFileNameOnFTA: String?,
        var dataPath: String?,
        var version: Int?,
        var source: String?,
        var numberOfRecords: Long?
)





我在服务中的功能

fun findAllByCreationDateJoinBatches(date: LocalDate): List<ReportWithBatches> {
        return reportRepository.findAllByCreationDateJoinBatches(date)
    }
        }

【问题讨论】:

  • 不要使用本机查询。学习 JPQL。以下是 JPQL 中有关联接的文档:docs.jboss.org/hibernate/orm/current/userguide/html_single/…。此外,在返回 JSON 的控制器和返回数据的查询之间,您可以并且应该编写将数据转换为所需对象的代码,以便发回所需的 JSON。 Yoru 查询实际上返回一个 List>,您可以将这个列表转换为您想要的任何内容。

标签: spring jpa kotlin spring-data-jpa


【解决方案1】:

正如 cmets 中正确说明的那样,您查询的返回类型是 List&lt;Array&lt;Any?&gt;&gt;,而不是 List&lt;Any&gt;

创建一个用作 DTO 的数据类并将结果映射到它:

data class ReportWithBatchInfo(val azureFileName : String, /* more field here */)

fun findAllByCreationDateJoinBatches(date: LocalDate): List<ReportWithBatchInfo> {
    return reportRepository.findAllByCreationDateJoinBatches(date).map {
        ReportWithBatchInfo(it[0] as String, /* more mappings here */)
    }
}

【讨论】:

  • 感谢 Alexey 的指导。所以我更新了我的代码,但我现在收到一个错误:没有找到能够从类型 [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] 转换为类型 [catpipe.report.model.ReportWithBatches ]] 有根本原因。如何更改查询以避免此错误?
  • 你的仓库的返回类型是什么?似乎 JPA 试图直接转换为您的类的错误,不应该是这种情况。
  • 我不确定返回类型是什么,我会尝试调试。
  • 您的存储库应该返回List&lt;Array&lt;Any?&gt;&gt;,而不是List&lt;ReportWithBatches&gt;List&lt;ReportWithBatches&gt; 仅在您手动映射后从服务返回。
  • 知道了!我只是对 localDate 转换有一个错误,会弄清楚的。谢谢!你很善良。
猜你喜欢
  • 1970-01-01
  • 2012-08-08
  • 1970-01-01
  • 2023-03-06
  • 2012-01-01
  • 2020-03-25
  • 1970-01-01
  • 2016-05-23
相关资源
最近更新 更多