【问题标题】:How to handle this Type MisMatch found List required Entity spring Boot JPA如何处理这种类型 MisMatch found List required Entity spring Boot JPA
【发布时间】:2022-01-03 04:25:55
【问题描述】:

我在我的控制器类中创建了一些操作,我想存储 我从操作中得到的结果,但是当我存储这些列表时 它向我显示的错误如下所示

Type mismatch.
Required:
DepositMaterial!
Found:
List<Result>

这是我的控制器类

@PatchMapping("/pendingValue")
    fun  pend(@ModelAttribute request:ReqFindPending):ResponseEntity<*>{
        val existingWithdraw = depositRepository.findPendingByWithDrawId(request.withDrawId)
        if (existingWithdraw != null){
           val upPending = depositRepository.getPending(withDrawId = request.withDrawId)
            depositRepository.save(upPending)
            return ResponseEntity(ResMessage("Pending update successfull"),HttpStatus.OK)
        }else{
            return ResponseEntity(ResMessage(" id getting null value"),HttpStatus.NOT_ACCEPTABLE)
        }

    }

我的仓库

package com.nilmani.workload.repository

import com.nilmani.workload.entity.DepositMaterial
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RequestParam

interface DepositRepository : JpaRepository<DepositMaterial, Long> {
    @Query("SELECT wm.quantity ,dd.totalDeposit,wm.quantity -dd.totalDeposit AS pending FROM WithdrawMaterial wm INNER JOIN  DepositMaterial dd ON wm.id = dd.withDrawId ")
    fun getPending(@Param("withDrawId")withDrawId: Long?):List<Result>
}

这是我的结果模型

data class Result(
    val pending: Long,
    val quantity: Long,
    val totalDeposit: Long
)

DepositMaterial 实体类

package com.nilmani.workload.entity

import com.nilmani.workload.enum.Material
import java.time.LocalDateTime
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
data class DepositMaterial (
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id:Long=-1,
    val withDrawId:Long=-1,
    val totalDeposit:Long=-1,
    val material:Int= Material.NONE.type,
    val depositTime:LocalDateTime = LocalDateTime.now(),
    val pending:Long = -1,
    val isDeposited:Boolean=false,
    )

这个问题是什么原因,我只想退回这三个 东西,并存储totalDeposit的结果减法结果和 待处理列中的数量以更新表但是,它给了我错误 存放押金材料

【问题讨论】:

    标签: spring-boot kotlin jpql


    【解决方案1】:

    您返回的是来自getPending 的结果列表,而不是单个的 DepositMaterial。但是save 方法需要一个对象来保存。尝试通过更改返回签名仅向getPending 请求一个对象。

    【讨论】:

      【解决方案2】:
      depositRepository.getPending
      

      返回实体列表。

      depositRepository.save(upPending)
      

      将单个实体保存在数据库中。

      解决方案:

      要么将您的save(upPending) 方法更改为saveAll(upPending),要么更新您的getPending repo 方法以返回一个唯一的实体对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-25
        • 2018-10-14
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 1970-01-01
        • 2018-03-11
        • 1970-01-01
        相关资源
        最近更新 更多