0

我在父对象上有这个

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

在我的控制器中,我有这个

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();
    }
}

它会更新父对象,但是当我更改子对象上的某些内容时,它不会更新它,也不会给休眠或 Oracle 带来问题。

有谁知道为什么它不更新子对象?

谢谢大家!

已更新解决方案!

这就是它对我的工作方式,因为@JB Nizet 说你也必须保存子对象。

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

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

另一种方法!

有了这个

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

我已经能够制作 oldUser.save() 并保存子对象。

4

1 回答 1

1

AFAIK,Play 需要调用save()所有修改后的实体。因此,您可能需要遍历用户的指示并保存它们:

for (Direction direction : user.getDirections()) {
    direction.save();
}
于 2012-07-04T12:23:02.450 回答