0

I´ve got this on the parent object

@OneToMany(mappedBy="idUser", cascade = CascadeType.MERGE)
public List<Directions> directions;

And in my controller I´ve got this

public static void userUpdate(String apikey, JsonObject body) {
    if(validate(apikey)) {

        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.em().merge(user);

        oldUser.save();

    }else{
        forbidden();
    }
}

It makes the update on the parent object but when I change something on the children object it doesn't update it and neither gives problems with hibernate or Oracle.

Does anyone know why it doesn´t update the child object?

Thanks all!

Updated with solution!

This is how it works for me, as @JB Nizet said you´ve got to save the child objects too.

oldUser.em().merge(user); 
oldUser.save();

for (Direction direction : oldUser.directions) { 
    direction.save();          
}

Another aproach!

With this

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "SASHNBR", insertable = true, updatable = true)
public List<Direction> directions;

I´ve been able to make oldUser.save() and get the child objects saved.

reixa
  • 6,903
  • 6
  • 49
  • 68
  • 1
    Did you look at http://stackoverflow.com/questions/2530498/jpa-persisting-object-parent-is-ok-but-child-not-updated – rayyildiz Jul 04 '12 at 12:06
  • @rayyildiz I did, if you look at the code I implemented the CascadeType.MERGE which is the one I want. Anyway I will try with CascadeType.ALL. Thanks! :) – reixa Jul 04 '12 at 12:09
  • @rayyildiz didn't work either. :( – reixa Jul 04 '12 at 12:11

1 Answers1

1

AFAIK, Play requires a call to save() on all the modified entities. So you probably need to iterate through the user's directions and save them as well:

for (Direction direction : user.getDirections()) {
    direction.save();
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thanks for the answer, that was all that I needed. For more detail: oldusuario.em().merge(usuario); oldusuario.save(); for (Direccion direccion : oldusuario.direccion) { direccion.save(); } – reixa Jul 04 '12 at 12:40