java - 删除实体会导致 hibernate 中的 ObjectDeletedException

标签 java hibernate exception mapping

我正在尝试在 hibernate 中映射 3 个实体之间的级联关系。这些实体是

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

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

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

我认为,这是因为套装项目同时属于制造商和分销商。我不确定如何正确建模/映射它。 有人可以帮助我吗?我真的正在学习 hibernate 的第一课。

真诚的

吉姆。

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

最佳答案

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

只需删除您试图从父级集合中删除的对象,或更改您的级联/逆映射。

关于java - 删除实体会导致 hibernate 中的 ObjectDeletedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6915102/

相关文章:

spring - 将 OneToOne/ManyToOne 与 JPA 和多个数据源一起使用时出错

python - 我如何捕捉一个像异常一样的 numpy 警告(不仅仅是为了测试)?

spring - 基于 Maven 的项目使用 Spring Hibernate、EJB3.0、JPA JBOSS?

java - 出现错误 java.lang.NoClassDefFoundError : com. google.android.gms.common.GooglePlayServicesUtil

java - java 程序中得到意外的输出

java - GWT 应用程序和 LifeRay 门户之间的通信

java ->>> 使用继承的 Hibernate 示例 <<<

java - 检测到错误时重新启动程序

java - 如何创建检查 int 范围、数字类型而不是 char 的异常?

java - 尝试为空记录找到真实条件