mysqlintegrityconstraintviolationexception:列“”不能为null

kuhbmx9i  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(504)
mapper:
<update id="updateSurplusAmountByPrimaryKeyAndMaterialTypeId" 
  parameterType="java.util.List">
    update db_logistics.table_inventory_material
    set surplusAmount=
    <foreach collection="list" item="item" index="index"
             separator=" " open="case" close="end">
        when inventoryId=#{item.inventoryId} and materialTypeId=# 
    {item.materialTypeId} then #{item.surplusAmount,jdbcType=INTEGER}
    </foreach>
    where inventoryId in
    <foreach collection="list" index="index" item="item"
             separator="," open="(" close=")">
        #{item.inventoryId,jdbcType=BIGINT}
    </foreach>
</update>
 fun updateSurplusAmountByPrimaryKeyAndMaterialTypeId(records: 
List<InventoryMaterial>): Int
data class InventoryMaterial(
    var inventoryId: Int = 0,
    var materialTypeId: Int = 0,
    var surplusAmount: Int = 0,
    var consumeSpeed: Float = 0f,
    var consumeAlarmDayCount: Int = 0,
    var updateDataTime: LocalDateTime =  LocalDateTime.now())

错误
“原因:com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception:列‘盈余金额’不能为空”
当我修改 when inventoryId=#{item.inventoryId} and materialTypeId=#{item.materialTypeId}when inventoryId=#{item.inventoryId} ,则错误消失。但是我需要两个参数inventoryid和materialtypeid来决定inventoryid。有人能给我一些答案吗?

bd1hkmkf

bd1hkmkf1#

执行的查询从中选择记录 table_inventory_materialinventoryId 并使用每个 materialTypeId .
问题的出现是因为 inventoryId 你在数据库中传递的列表 materialTypeId 在作为参数传递的列表中提供的。
所以生成的case语句 NULL 为了失踪的人 materialTypeId 以及试图 surplusAmountNULL 导致错误。

相关问题