【问题标题】:Using @PatchRequest to update only certain fields使用 @PatchRequest 仅更新某些字段
【发布时间】:2017-12-21 09:58:47
【问题描述】:

我尝试使用 @PatchRequest 测试我的 API,但出现以下错误。

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/intranet-api] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute batch; SQL [update portal.dbo.DANCE_MASTER set name=?, age=?, gender=?, grade=?,status=?, updated_by=?, updated_ts=? where id=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute batch] with root cause
    java.sql.BatchUpdateException: Cannot insert the value NULL into column 'name', table 'ctcportal1.dbo.DANCE_MASTER'; column does not allow nulls. UPDATE fails.

从显示的错误来看,我似乎应该为所有字段提供更新的值。但是,我只想更新表中的某些字段。

【问题讨论】:

    标签: spring rest api


    【解决方案1】:

    java.sql.BatchUpdateException:无法将值 NULL 插入列 '名称',表'ctcportal1.dbo.DANCE_MASTER';列不允许 空值。更新失败。

    错误消息明确指出,无法将值 NULL 插入列'name'。 检查 DANCE_MASTER 表定义并为所有必需的列提供值。

    您还可以通过对数据库运行以下查询来获取值。用适当的值替换问号。

    update portal.dbo.DANCE_MASTER 
    set name=?, 
    age=?, 
    gender=?, 
    grade=?,
    status=?, 
    updated_by=?, 
    updated_ts=? 
    where id=?
    

    【讨论】:

    • 嗨,但我只想更新某些字段,而不是所有字段。我可以知道我该怎么做。这就是我使用 patchRequest 而不是 putRequest 或 PostRequest 的原因。
    • 然后分享一下spring dao代码相关更新记录的代码。
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多