Spring Boot PUT操作正在触发对数据库中所有列的查询

时间:2019-04-15 06:49:04

标签: java spring spring-boot

使用Spring框架,通过仅更改有效负载中的一个属性来尝试执行更新rest操作

@RequestMapping(value = "/uiv/activeDiscrepancies/{id}", method = RequestMethod.PUT)
public ResponseEntity update(@PathVariable("id") BigInteger id,
                             @RequestBody ActiveDiscrepancies activeDiscrepancy)
                   throws SureException{

    //ActiveDiscrepancies adiscrepancies =  
    activeDiscrepancyRepo.save(activeDiscrepancy, id);

    ActiveDiscrepancies adiscrepancies = activeDiscrepancyRepo.findByActiveDiscrepancyId(id); 
    activeDiscrepancy.setActiveDiscrepancyId( adiscrepancies.getActiveDiscrepancyId() );
    activeDiscrepancyRepo.save(activeDiscrepancy);
}

但是触发了更新查询

休眠:

update ACTIVE_DISCREPANCIES 
   set ACTIVE_DISCREPANCY_NAME=?, AUDITED_ENTITY=?, CREATION_TIME=?, 
       DELTA_TYPE=?, DISCREPANCY=?, EXPIRY_TIME=?, LAST_UPDATED_TIME=?, 
       MESSAGE_KEY=?, NETWORK=?, RECON_REASON=?, RECON_TYPE=?, 
       REMARKS=?, RESOURCE_LABEL=?, STATUS=? 
 where ACTIVE_DISCREPANCY_ID=?

那么对于其他列替换为空值,如何解决此问题?

1 个答案:

答案 0 :(得分:1)

首先,PUT方法用于接收和更新整个实体。对于部分更新,最好使用PATCH方法。您可以了解有关here的更多信息。

如果您希望更新整个实体,则应在请求正文中发送整个实体。如果不是,那么您应该按ID从db获取实体,并相应地应用更改并保存。 Spring Data存储库中的save方法将替换整个实体。