简体   繁体   English

使用Hibernate删除一对多关系中的子对象/记录

[英]Delete child objects/records in one-to-many relationship using Hibernate

I have a parent-child (one-to-many) relationship between product & packages. 我在产品和包装之间存在亲子关系(一对多)。 one product can have multiple packages. 一个产品可以有多个包装。 In my 'Edit Product' jsp page I am allowing user to edit product & delete/add/edit any package. 在我的“编辑产品” jsp页面中,我允许用户编辑产品并删除/添加/编辑任何软件包。 Now, on submitting the changes I am doing saveOrUpdate(product) in my controller method. 现在,在提交更改时,我正在控制器方法中执行saveOrUpdate(product)。 What I am observing is that new packages are getting added in DB but the ones that are deleted by user hence not in the packages Set of Product are not getting deleted from DB. 我正在观察的是,新软件包已添加到数据库中,但用户删除的那些软件包因此不在产品集软件包中,而没有从数据库中删除。 My hiberate configurations snippet is below. 我的休眠配置代码段如下。 Am I missing something here? 我在这里想念什么吗?

<hibernate-mapping>
    <class name="author.vo.ProductVO" table="Product">
    <id name="ProductID" type="long">
            <column name="Product_ID" />
            <generator class="increment" />
     </id>
    <set name="packages" inverse="true" cascade="all" lazy="false">
    <key column="Product_ID" not-null="true" on-delete="cascade"/>
    <one-to-many class="author.vo.PackageVO" />
     </set>

Code for adding product 添加产品的代码

@Autowired
private HibernateTemplate hibernateTemplate;

this.hibernateTemplate.saveOrUpdate(prod);

You should add "cascade delete orphan" to your xml config, also, why dont you try a simple situation first of all. 您还应该在XML配置中添加“层叠删除孤立”,为什么不首先尝试简单的情况。

  1. is a save working with a parent - child. 是与父母(子女)一起工作的储蓄。
  2. is a delete working with a parent - child. 是与父子一起工作的删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM