【问题标题】:JPA, merge objects updating childrensJPA,合并对象更新子对象
【发布时间】:2012-07-04 11:54:47
【问题描述】:

我在父对象上有这个

@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() 并保存子对象。

【问题讨论】:

  • @rayyildiz 我做了,如果你看一下我实现 CascadeType.MERGE 的代码,这是我想要的。无论如何,我会尝试使用 CascadeType.ALL。谢谢! :)
  • @rayyildiz 也不起作用。 :(

标签: oracle hibernate jpa playframework


【解决方案1】:

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

for (Direction direction : user.getDirections()) {
    direction.save();
}

【讨论】:

  • 感谢您的回答,这就是我所需要的。更多细节:oldusuario.em().merge(usuario); oldusuario.save();对于(方向方向:oldusuario.direccion){ direccion.save(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 2021-01-17
相关资源
最近更新 更多