使用@PATCH方法的Spring REST部分更新 [英] Spring REST partial update with @PATCH method

查看:58
本文介绍了使用@PATCH方法的Spring REST部分更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据以下内容对Manager实体实施部分更新:

I'm trying to implement a partial update of the Manager entity based in the following:

实体

public class Manager {
    private int id;
    private String firstname;
    private String lastname;
    private String username;
    private String password;

    // getters and setters omitted
}

Controller中的SaveManager方法

SaveManager method in Controller

@RequestMapping(value = "/save", method = RequestMethod.PATCH)
public @ResponseBody void saveManager(@RequestBody Manager manager){
    managerService.saveManager(manager);
}

在Dao impl中保存对象管理器.

Save object manager in Dao impl.

@Override
public void saveManager(Manager manager) {  
    sessionFactory.getCurrentSession().saveOrUpdate(manager);
}

当我保存对象时,用户名和密码已正确更改,但其他值均为空.

When I save the object the username and password has changed correctly but the others values are empty.

所以我需要做的是更新用户名和密码,并保留所有剩余数据.

So what I need to do is update the username and password and keep all the remaining data.

推荐答案

您可以编写仅更新特定字段的自定义更新查询:

You can write custom update query which updates only particular fields:

@Override
public void saveManager(Manager manager) {  
    Query query = sessionFactory.getCurrentSession().createQuery("update Manager set username = :username, password = :password where id = :id");
    query.setParameter("username", manager.getUsername());
    query.setParameter("password", manager.getPassword());
    query.setParameter("id", manager.getId());
    query.executeUpdate();
}

这篇关于使用@PATCH方法的Spring REST部分更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆