Spring Data JPA:如何优雅地更新模型? [英] Spring Data JPA: How to update a model elegantly?

查看:257
本文介绍了Spring Data JPA:如何优雅地更新模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我的模型是这样的:

  1. My Model is like this:

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name="email")
    private String email;

    @Column(name = "weblink")
    private String webLink;

    //getter & setter
}

  • 我们通过http请求收集表单或移动数据,springmvc会将这些数据发送给像用户这样的模型.

  • we collect form or mobile data via http request, and springmvc will make these data to a Model like user.

    例如,我有这样的请求:

    for example, I have a request like this:

    http://localhost:8080/update?id=1919&email=xx@google.com

    在contoller中,请求url及其参数将自动转换为User对象.

    in contoller, the request url and its paramters will be automaticlly trans to a User object.

    @RequestMapping(method = RequestMethod.GET, value = "/update0")
    public User update(@ModelAttribute("User") User user){
    
        System.out.println(user.getId());
        System.out.println(user.getEmail());
        System.out.println(user.getWebLink());
    
        return userRepository.save(test);
    }
    

  • 如果我在mysql中有一个ID为1919的记录,并且列(id,email,weblik)都具有值.

  • if I have a record in mysql which id is 1919, and the columns (id, email, weblik) are all have values.

    如您所见,通过网络或移动设备传递的用户对象具有两个属性

    as you see, the user object which passed by web or mobile have two properties

    http://localhost:8080/update?id=1919&email=xx@google.com

    id和电子邮件具有值,而Weblink没有值.

    id and email have values and weblink do not have.

    因此,如果我执行save方法,则电子邮件列将更新为xx@google.com,并且weblik字段也将更新为NULL,但是我不想更新此字段,我只是要更新电子邮件字段.

    So, if i execute save method, the columns email will be updated to xx@google.com, and the weblik field will also be updated to NULL, but I don't want to update this field, I just want to update email field.

    我有两种方法可以解决此问题,但都不是很好.

    I have two ways to fix the problem, but all of them are not elegant.

    5.1首先加载用户对象并更新

    5.1 load user object first and update

    User userExist = userRepository.findOne(user.getId());
    userExist.setEmail(user.getEmail());
    
    //or using 
    //BeanUtil.copyProprty(formDto,modle)
    
    userRepository.save();
    

    5.2使用@DynamicUpdate,但无法正常工作.

    5.2 using @DynamicUpdate, but its not working.

    还有其他方法可以更新用户模型,而无需执行其他任何工作.

    Is there other way to update the User Model and do not do some extra work.

    预先感谢.

    推荐答案

    最简单的方法是适当设置控制器方法:

    The easiest way is an appropriately set up controller method:

    @RequestMapping(value = "/users/{user}", method = RequestMethod.PATCH)
    public … updateUser(@ModelAttribute User user) { … }
    

    根据参考文档,当调用此方法时,将发生以下步骤:

    According to the reference documentation when this method is called the following steps happen:

    1. 需要获取User的实例.这基本上需要从StringUserConverter向Spring MVC注册,才能将从URI模板提取的路径段转换为User.如果您是使用Spring Data并按照其
    1. An instance of User needs to be obtained. This basically requires a Converter from String to User to be registered with Spring MVC to convert the path segment extracted from the URI template into a User. If you're e.g. using Spring Data and enable its web support as described in its reference documentation, this will work out of the box.
    2. After the existing instance was obtained, request data will be bound to the existing object.
    3. The bound object will be handed into the method.

    其他提示

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