首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除实体导致hibernate中的ObjectDeletedException

删除实体导致hibernate中的ObjectDeletedException
EN

Stack Overflow用户
提问于 2011-08-03 00:10:28
回答 2查看 1.5K关注 0票数 0

我正在尝试映射hibernate.The实体中3个实体之间的级联关系

代码语言:javascript
复制
1.Item --has a Maker and a Distributor.
2.Maker --has a set of items created by him.
3.Distributor ---has a set of items he distributes.

需要的关系:

代码语言:javascript
复制
1.When an Item is saved/updated ,its Maker and Distributor should be saved/updated
2.When a Maker is deleted ,all his items should be deleted.
3.When a Distributor is deleted,all his items should be deleted.

我试过这样做,

代码语言:javascript
复制
class Item{
    private Long item_id;
    ...
    private Maker maker;
    private Distributor distributor;
    ...
}
Item.hbm.xml
...
   <!--
    when Item is saved the associated Distributor is also saved
    -->
   <many-to-one name="distributor" class="Distributor" column="DISTRIBUTOR_ID" lazy="false" cascade="save-update"/>
    <!--
    when Item is saved the associated Maker is also saved
    -->
    <many-to-one name="maker" class="Maker" column="MAKER_ID" lazy="false" cascade="save-update"/>
...

class Maker{
    private Long maker_id;
    ...
    Set<Item> items;
    public Maker(){
        items = new HashSet<Item>();
    }
...
}
Maker.hbm.xml
...
<!--
when a Maker is saved,all his items are saved.
when a Maker is deleted,all his items are deleted.
-->
<set name="items" inverse="true" table="ITEM" lazy="false"  cascade="all,delete-orphan">
            <key column="MAKER_ID" />
            <one-to-many class="Item" />
</set>
...


class Distributor{
    private Long distributor_id;
    ...
    Set<Item> items;
    public Distributor(){
        items = new HashSet<Item>();
    }
    ...
}

Distributor.hbm.xml
<!--when a Distributor is saved, all his items are saved.
    when a Distributor is deleted, all his items are deleted
-->
<set name="items" inverse="true" table="ITEM" lazy="false" cascade="all,delete-orphan">
<key column="DISTRIBUTOR_ID" />
<one-to-many class="Item" />
</set>
...

然后我创建了一些实例,并尝试找出删除一个Maker是否会删除他的所有项目。然而,当我尝试这样做时,我得到了这个错误

代码语言:javascript
复制
hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [myapp.domain.Item#27]

我想,这是因为集合项目同时属于Maker和Distributor.I。我不确定如何正确地建模/映射它。有人能帮帮我吗?我真的在上hibernate的第一课。

由衷地

吉姆。

代码语言:javascript
复制
main(..){
  createEntities();
  deleteSomeEntities();
}

public void createEntities(){
   session = HibernateUtil.getCurrentSession();
   Transaction tx = null;

   Maker maker1 = new Maker();
   maker1.setName("maker1");

   Distributor dis1 = new Distributor();
   dis1.setName("dis1");

   Item item1 = new Item();
   item1.setName("item1");
   item1.setMaker(maker1);
   item1.setDistributor(dis1);

   Item item2 = new Item();
   item2.setName("item2");
   item2.setMaker(maker1);
   item2.setDistributor(dis1);

   Set<Item> items = new HashSet<Item>();
   items.add(item1);
   items.add(item2);
   maker1.setItems(items);
   dis1.setItems(items);
   try{
        itemdao.saveOrUpdate(item1);
        itemdao.saveOrUpdate(item2);

    }catch(RuntimeException e){
        logger.error("rolling back"+e.getMessage());
        tx.rollback();
        throw e;
    }
}

public void deleteSomeEntities(){
    session = HibernateUtil.getCurrentSession();
    Transaction tx = null;
    try{
        Maker maker = makerdao.findMakerByName("maker1");
        String name = maker.getName();
        logger.info("got maker:"+name);
        makerdao.deleteMaker(maker);
        tx.commit();
    }catch(RuntimeException e){
        logger.info("rolling back");
        tx.rollback();
        throw e;
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-03 00:13:34

每当您收到此错误时,都是因为您正在尝试删除一个对象,但该对象在某些父对象列表中被引用,并且您的传递持久性(即级联)设置被设置为父对象控制关系。换句话说,您向hibernate发出了相互冲突的命令:您告诉hibernate删除对象,但同时又告诉它,如果对象在指定的集合中,则执行保存操作。

只需从父对象的集合中删除您要删除的对象,或者更改您的级联/反向映射。

票数 1
EN

Stack Overflow用户

发布于 2011-08-03 01:05:23

我想你会发现这是你的映射上的级联。

Cascade=“保存-更新”

尝试cascade="all“,然后Hibernate应该会为你做这件事。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6915102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档