【发布时间】: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