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.
精彩评论