开发者

Different behavior of hibernate merge, and saveOrUpdate

in one particular case, i have to use merge instead of saveOrUpdate.

//
// item is detached object.
//
Category category = item.getCategor开发者_开发技巧y();
category.setName("cloth");
item.setName("shirt");
session.merge(item);

the thing is category name doesn't get updated while using merge, but it gets updated while using saveOrUpdate. anyone can explain why?


You must have @ManyToOne(cascade=CascadeType.MERGE) (at least) for that to work (or the relevant xml mapping)

This instructs hibernate to cascade merge operations to this particular relation - i.e. when the parent object (item) is merged, also merge the child (category). Maybe you have omitted the MERGE cascade type, and that's why saveOrUpdate works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜