java 如何仅更新已更改的属性 - Spring MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35357267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:03:08  来源:igfitidea点击:

How to update only attributes that have changed - Spring MVC

javaspringmodel-view-controller

提问by melkir

My controller

我的控制器

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String updateUserById(@PathVariable("id") Long id, Model model) {
    User user = userRepository.findOne(id);
    model.addAttribute(user);
    return "admin/editUser";
}

@RequestMapping(value = "/user/{id}", method = RequestMethod.POST)
@ResponseBody
public String updateUserById(@PathVariable("id") Long id, @ModelAttribute User user) {
    userRepository.updateUser(id, user); // with a try catch
}

The dao

@Override
public void updateUser(Long id, User user) {
    User userDB = userRepository.findOne(id);
    userDB.setFirstName(user.getFirstName());
    userDB.setLastName(user.getLastName());
    userDB.setEmail(user.getEmail());
    userDB.setUsername(user.getUsername());
    userRepository.save(userDB);
}

This method works but it's pretty ugly for me. Let's say that the user have just changed the firstname field in the view, how can I adapt my code to only call the function to set the firstname ?

这种方法有效,但对我来说很丑陋。假设用户刚刚更改了视图中的 firstname 字段,我该如何调整我的代码以仅调用函数来设置 firstname ?

Something like the Observer pattern to notify field that have change ?

像观察者模式一样通知有变化的字段?

采纳答案by Patrick

I suppose that your code works like this:

我想你的代码是这样工作的:

An user want to change some data of his user entity. Means in frontend you already show him all his entries from the user table of the DB like firstname, lastname and so on.

一个用户想要改变他的一些数据user entity。意味着在前端,您已经向他展示了他从数据库的用户表中的所有条目,如名字、姓氏等。

For example the user changed his firstname and hit the save button. Your controller receive the user entity inclusive the user.id, the new firstname and all the old set values. If your code works like this you can easily save()the user entity. There is no need to fetch the user from the DB first.

例如,用户更改了他的名字并点击了保存按钮。您的控制器接收用户实体,包括 user.id、新名字和所有旧设置值。如果您的代码像这样工作,您可以轻松地save()访问用户实体。无需先从数据库中获取用户。

So just do it like this:

所以就这样做:

@Override
public void updateUser(User user) {
    userRepository.save(user);
}

The Repositoryknows the Id of the user entity and just update the complete entity.

Repository知道用户实体的ID和刚刚更新完整的实体。

And for the reason you dont have the Id in your user entity use the Id from your controller like this:

由于您的用户实体中没有 Id 的原因,请使用控制器中的 Id,如下所示:

@Override
public void updateUser(User user, Long Id) {
    user.setId(Id);
    userRepository.save(user);
}

回答by Musaddique

if you are using hibernate then there is one attribute in hibernate dynamicUpdate = truewhich will update only updated fields in db

如果您使用的是 hibernate,那么 hibernate 中有一个属性dynamicUpdate = true将仅更新 db 中的更新字段

like below code

像下面的代码

@Entity
@Table(name = "User")
@org.hibernate.annotations.Entity(
        dynamicUpdate = true
)
public class User 

for hibenrate 4 + use these

对于休眠 4 + 使用这些

@DynamicInsert(true)
@DynamicUpdate(true)