【问题标题】:Is there anyway to remove db entry if saved object is not present in updated client request?如果更新的客户端请求中不存在保存的对象,是否有删除数据库条目?
【发布时间】:2020-05-09 09:54:40
【问题描述】:

例如,如果 db 条目是使用请求负载数组中的两个对象创建的。

Request : {"users":[{"identifier":"abctest","name":"uan"},{"identifier":"deftest","name":"Aj"}]}

Response : {"users":[{"id":182,"identifier":"abctest","name":"uan"},{"id":301,"identifier":"deftest","name":"Aj"}]}

然后从前端删除该数组的一个对象,并尝试发送仅包含一个对象而不是两个请求负载的请求。

Updated Request : {"users":[{"id":182,"identifier":"abctest","name":"uan"}]}

Response :  {"users":[{"id":182,"identifier":"abctest","name":"uan"},{"id":301,"identifier":"deftest","name":"Aj"}]}

Expected response : {"users":[{"id":182,"identifier":"abctest","name":"uan"}]}

如何删除保存在数据库中但不存在于请求负载中的那个对象?

抱歉,如果我遗漏了什么,我是 Java 新手。

【问题讨论】:

  • 代码 sn-ps,请求有效负载结构将有助于回答问题,请编辑问题并添加它们。另外,请花点时间阅读How to ask good questions?

标签: java arrays spring postgresql hibernate


【解决方案1】:

有两种方法可以做到这一点,具体取决于您的要求:

  • 删除表中的所有条目并从负载中插入新用户。
  • 创建一个新表映射用户ID和管理员ID(或发送请求创建这些用户的主用户)

所以现在每次特定用户/管理员发送创建用户的请求时,您只需根据该请求者的 id 从映射表中删除并插入新用户

【讨论】:

  • 感谢您回答拉胡尔。如果你能提供一些代码示例来理解实现会很有帮助?
猜你喜欢
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多