I´ve got a controller which gets an JsonObject from a POST method, parses it to a Model object and saves it.
The main problem is that I have to use the database object, apply the changes over it with the new object values and then save the old one. This wouldn’t be a problem if they where 2 properties but User is a big object and I want to make some clean code there.
This is the code:
public static void userUpdate(String apikey, JsonObject body) {
Long idUser = decode(apikey);
User oldUser= User.findById(idUser);
Map<String, User> userMap = new HashMap<String, User>();
Type arrayListType = new TypeToken<Map<String, User>>(){}.getType();
userMap = gson().fromJson(body, arrayListType);
User user = userMap.get("user");
oldUser.cif = user.cif;
oldUser.date_last_mod = user.date_last_mod;
oldUser.save();
}
Is there a way to make a merge(oldUser, user) and make oldUser.save()? Or any other ideas?
Thanks all for the help! 🙂
I solved it like this.
You can get the EntityManager to make use of the merge option, I´ve to make some test but the first conclusion I make is that the object you are calling the merge for is the one who´s the slave and the one you pass as parameter is the master object which will update oldUser with the new values.
Thanks anyway 🙂