为什么Hibernate update()方法不会删除孤儿,而merge()会删除孤儿

时间:2018-07-03 12:57:49

标签: java hibernate

我是冬眠的新手,试图定义冬眠为什么不删除DETACHED对象的孤儿(merge()这样做)

我的代码:

public class Admin {
    @OneToMany(mappedBy="adminId", orphanRemoval = true  )         
    @Cascade(value={org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    private Set<AdminRole> roles = new LinkedHashSet<>();
...

我的测试如下:

    public void test2(){
        Admin sysAdmin = new Admin();
        sysAdmin.setLastName("TEST");
        AdminRole role = new AdminRole("FULL_ADMIN", sysAdmin);
        sysAdmin.getRoles().add(role);

        Session session = sessionFactory.getCurrentSession();
        session.save(sysAdmin);  //here roles successfully saved
        session.evict(sysAdmin);  //detaching the obj

        sysAdmin.getRoles().remove(role); //removing role
        session.update(sysAdmin); // this code doesn't delete orphans (can't get why)
        //session.merge(sysAdmin); //this deletes orphans succesfully

有人可以解释一下,或者给出一个链接,为什么会这样? 我应该怎么做才能使更新生效?还是只使用merge()? 谢谢。

0 个答案:

没有答案