开发者

Different behavior of hibernate merge, and saveOrUpdate

开发者 https://www.devze.com 2023-02-17 05:06 出处:网络
in one particular case, i have to use merge instead of saveOrUpdate. // // item is detached object. // Category category = item.getCategor开发者_开发技巧y();

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

精彩评论

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