I have a pretty simple mysql record like this:
+------+-------+-----------+
| id | name | password |
+------+-------+-----------+
| 1 | John | d0c91f13f |
+------+-------+-----------+
... ... ...
And here is its hibernate entity; nothing fancy
@Entity
@Table(name = "user", schema = "", catalog = "trade")
public class UserEntity{
private long id;
private String name;
private String password;
@Id
@Column(name = "id")
public long getId(){
return id;
}
public void setId(long id){
this.id = id;
}
@Column(name = "name")
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@Column(name = "password")
public String getPasswrod(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
For convenience, I use Gson to parse the entity from json string which front-end passed in.
The json string for the record is like this:
{"id":1, "name":"John", "password":"d0c91f13f"}
then userEntity will be parsed from the json String:
UserEntity userEntity = gson.fromJson(userJson, UserEntity.class);
I can insert or update the user with Session.save(userEntity)
and Session.update(userEntity)
.
If every field is contained in the json string, then things seemed goes as expected.
But when some field, such as password
is omitted:
{"id":1, "name":"John Smith"}
which indicated that I should make a partial update and leave the omitted field not modified, things went wrong. Because
the parsing procedure will set password
to Null. and update it to the database.
So, is there a solution to partially update the record in this case?
Going through every field and setting fields one by one will be the last option; anything other than that?
Thanks in advance.