休眠。在更新子列表上删除 [英] Hibernate. Remove on update child list

查看:100
本文介绍了休眠。在更新子列表上删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate中有标准的 @ManyToOne 关联。更新时,我创建了新的实体(使用 new 关键字),并填入必要的值(也插入了ID)。值来自UI。以同样的方式创建新的子对象集合,通过值填充每个对象(ID也插入),并将集合存储在父对象中(使用setter或 addAll()方法)。

所以我的问题是:如何在更新父项时删除父集合中的所有对象,并将其替换为新集合。在新集合中,一些对象是新的,并且只需要更新(它们具有插入的ID)。



我了解了 orhanRemoval ,但它无法帮助,因为父对象必须处于托管状态(所以 clear()在子集合上将不起作用),而不是暂时状态,如我的示例中所示。

saveOrUpdate() / em>方法不考虑数据库内对象的当前状态,而是更新对象的所有当前属性。因此,在你的情况下,当你用它的集合更新新对象时,它不会照顾旧集合,它会更新对象的新集合。因此,您在数据库中同时拥有旧集合和新集合。



要用新集合覆盖旧集合,您应该使用 merge )方法,它首先将具有相同id的对象加载到持久化上下文中,然后将分离对象的状态复制到持久化对象的状态,然后考虑该对象是否脏。如果是这样的话,它会保持新对象的变化。



以下代码可能会演示上面的解释:

  //初始化持久层
DAOLayer daoLayer = new DAOLayer();

//用一个孩子持有父对象
父亲=新父(父);
parent.addChild(new Child(child));
父母persistentParent = daoLayer.merge(父母);

//使用存储在DB
中的相同标识创建新的父对象newParent = new Parent(parent);
newParent.setId(persistentParent.getId());
newParent.addChild(new Child(child));

//更新新的父对象
persistentParent = daoLayer.merge(newParent);

上面的代码在数据库中导致1个孩子。如果将merge()方法更改为saveOrUpdate()方法,则会导致数据库中有两个孩子。


I have standard @ManyToOne association in Hibernate. When updating, I'm creating new entity (using new keyword) and fill it with necessary values (ID also inserted). Values comes from UI. In same way I create new collection of child objects, fill each of them by values (ID also inserted), and store collection in parent object (using setter or by addAll() method).

So my question is: how I can remove all objects in parent collection when updating parent, and replace them by new collection. In new collection some objects are really new and some only need to be updated (they have inserted ID).

I learned about orhanRemoval, but it can't help, because parent object must be in "managed" state (so clear() on child collection will not work), not in transient state as in my example..

解决方案

The update or saveOrUpdate() method does not take care the current status of the object inside the database, instead it updates all current attributes of the object. Therefore in your case, when you update the new object with its collection, it does not take care the old collection and it will update the new collection of the object. Hence, you have both the old collection and the new collection inside the database.

To overwrite the old collection by the new one, you should use the merge() method, which at first it loads the object with the same id into the persistence context, then it copies the state of the detached object to the persistent one, then it considers if the object is dirty. If so, it will persists the new object with changes.

The following code may demonstrate the above explanation:

    // Initial the persistent layer
    DAOLayer daoLayer = new DAOLayer();

    // Persist the parent object with 1 child
    Parent parent = new Parent("parent");
    parent.addChild(new Child("child"));
    Parent persistentParent = daoLayer.merge(parent);

    // Create the new parent object with the same Id stored in DB
    Parent newParent = new Parent("parent");
    newParent.setId(persistentParent.getId());
    newParent.addChild(new Child("child"));

    // Update the new parent object
    persistentParent = daoLayer.merge(newParent);

The above code results in 1 child in the database. If you change the merge() method to the saveOrUpdate() method, it results in 2 children in the database.

这篇关于休眠。在更新子列表上删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆