Spring Data JPA:如何优雅地更新模型? [英] Spring Data JPA: How to update a model elegantly?
问题描述
-
我的模型是这样的:
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:
- An instance of
User
needs to be obtained. This basically requires aConverter
fromString
toUser
to be registered with Spring MVC to convert the path segment extracted from the URI template into aUser
. 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. - After the existing instance was obtained, request data will be bound to the existing object.
- The bound object will be handed into the method.
其他提示
- 请勿将
GET
用作更新的HTTP方法.GET
被定义为安全操作(无副作用),而更新则不是.PATCH
是正确的方法,因为它已定义为允许对现有资源进行部分更新. - 要将表单数据提交到
PUT
和PATCH
请求中,您需要按HttpPutFormContentFilter/spring-framework-reference/html/mvc.html#mvc-ann-form-urlencoded-data"rel =" noreferrer>此处.我向Spring Boot提交了问题,以进行默认注册. - Don't use
GET
as HTTP method for updates.GET
is defined to be a safe operation (no side effects), which an update is not.PATCH
is the correct method here as it's defined to allow partial updates to an existing resource. - To submit form data into
PUT
andPATCH
requests, you need to register anHttpPutFormContentFilter
with the application as described here. I filed an issue with Spring Boot to register that by default.
Additional hints
这篇关于Spring Data JPA:如何优雅地更新模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!