Spring Data JPA中基于内部联接查询和基于接口的投影

时间:2018-09-22 15:11:52

标签: mysql spring kotlin spring-data-jpa

我正在使用运行MySql DB的Spring Data JPA进行Spring MVC项目,其中有四个实体对象:旅行,费用,货币和资金。

这是我的数据库架构的直观表示:

enter image description here

在ExpenseRepository接口中,我扩展了JpaRepository接口。

现在,我正在尝试运行一个本机SQL查询,在此我将传递expenseId,并从Expense表中获取费用和金额,并从Currency表中获取currency_name。 (您可以看到我必须做两个内部联接才能获得currency_name。)

最后,我创建了另一个接口ExpenseOutput,将三列合并为一个单独的非实体接口(根据Spring Data JPA文档中提到的基于接口的投影映射)。

以下是代码:

package com.binarycraftbd.ksktravelbackend.Repo

import com.binarycraftbd.ksktravelbackend.JoinQueries.ExpenseData
import com.binarycraftbd.ksktravelbackend.Model.Expense
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface ExpenseRepo : JpaRepository<Expense, Int> {


    @Query("select expense, amount from expense, currencyName from currency inner join fund on expense.fund_id=fund.id inner join currency on fund.currency_id=currency.id where expense.id=?1", nativeQuery = true)
    fun getCurrencyByExpId(expId:Int): ExpenseOutput

    interface ExpenseOutput{
        fun getExpense():String
        fun getAmount(): String
        fun getCurrencyname(): String
    }
}

但是,当我通过RestController函数运行代码时,出现以下错误:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Sep 22 20:51:25 BDT 2018
There was an unexpected error (type=Internal Server Error, status=500).
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

我也在这里提供实体类:

旅行舱

@Entity
@Table(name = "travel")
class Travel(
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        val id: Int=0,
        val travelName: String="",

        @OneToMany(mappedBy = "travel")
        @JsonIgnore
        val funds: List<Fund> ?= null,

        @OneToMany(mappedBy = "travel")
        @JsonIgnore
        val expenses: List<Expense>?=null

)

货币类

@Entity
@Table(name = "currency")
class Currency(

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        val id: Int=0,
        val currencyName: String="",

        @OneToMany(mappedBy = "currency")
        @JsonIgnore
        val funds: List<Fund>?=null
)

基金类别

@Entity
class Fund(
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        val id:Int=0,
        val fundName:String="",

        @OneToMany(mappedBy = "fund")
        @JsonIgnore
        val expenses: List<Expense>?= null,

        @ManyToOne
        val travel: Travel?=null,

        @ManyToOne
        val currency:Currency?=null
)

费用等级

@Entity
class Expense(


        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        val id:Int=0,
        val date:String="",
        val time:String="",
        val expense:String="",
        val amount:String="",
        val category:String="",

        @ManyToOne
        val travel: Travel?=null,

        @ManyToOne
        val fund: Fund?=null
)

如何解决这个问题?在ExpenseRepository中编写查询代码是否不正确?还是Sql查询有问题?请帮助!!

1 个答案:

答案 0 :(得分:0)

Econ在将其嵌入代码之前,尝试在任何SQL客户端(例如mysql workbench)中运行此查询。

我像下面这样重写上面的查询,发现一些语法错误和非逻辑性内容。

select expense.expense, expense.amount, currency.currency_name  
from expense inner join fund on (expense.fund_id=fund.id)
inner join currency on (fund.currency_id=currency.id)
where expense.id=<replace this segment with a valid expenseId>