2011-08-02 76 views
0

我试图在hibernate.The实体3个实体之间的级联关系映射是删除实体导致ObjectDeletedException休眠

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. 

关系需要:

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. 

我试过这样的,

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> 
... 

然后我创建了一些实例,并试图找出是否删除一个制造商删除所有他项目.. 然而,当我试试这个,我得到这个错误

hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [myapp.domain.Item#27] 

我认为,这是因为设置项属于两个Maker和Distributor.I我不知道如何来模拟/正确映射此。 有人可以帮助我吗?我真的带着我的第一个课程与休眠。

sincerely

Jim。

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; 
    } 
} 

回答

1

每当你得到这个错误,它因为你试图删除的对象,但该对象被引用在一些父对象列表,你传递的持久性(即级联)设置设置使得母控制关系。换句话说,你正在给hibernate冲突的命令:你告诉它删除该对象,但是你也告诉它,如果该对象在指定的集合中,则进行保存。

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

+0

这意味着我将不得不从两个父母,制造商和分销商..清除项目尝试..谢谢 – jimgardener

+0

@jimgardener,是的。或者改变你的级联设置。 – hvgotcodes

0

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

级联=“保存更新”

尝试级联=“所有”,然后休眠应该为你做它。