有没有一种方法可以使用 Spring Data JPA中的方法 save
来更新实体对象的 只有一些田地?
例如,我有一个这样的 JPA 实体:
@Entity
public class User {
@Id
private Long id;
@NotNull
private String login;
@Id
private String name;
// getter / setter
// ...
}
通过 CRUD 的回购:
public interface UserRepository extends CrudRepository<User, Long> { }
在 春季 MVC中,我有一个控制器,它可以得到一个 User
对象来更新它:
@RequestMapping(value = "/rest/user", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<?> updateUser(@RequestBody User user) {
// Assuming that user have its id and it is already stored in the database,
// and user.login is null since I don't want to change it,
// while user.name have the new value
// I would update only its name while the login value should keep the value
// in the database
userRepository.save(user);
// ...
}
I know that I could load the user using findOne
, then change its name and update it using save
... But if I have 100 fields and I want to update 50 of them it could be very annoying change each value..
有没有办法告诉像“ 保存对象时跳过所有空值”这样的东西?