# How to soft delete a child entity to allow the client to review it before it finally gets deleted

**URL:** https://discourse.hibernate.org/t/how-to-soft-delete-a-child-entity-to-allow-the-client-to-review-it-before-it-finally-gets-deleted/1695
**Category:** Hibernate ORM
**Created:** [November 13, 2018, 1:52pm UTC](https://discourse.hibernate.org/t/how-to-soft-delete-a-child-entity-to-allow-the-client-to-review-it-before-it-finally-gets-deleted/1695 "2018-11-13T13:52:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![guibernardi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/guibernardi/32/90_2.png) [@guibernardi](https://discourse.hibernate.org/u/guibernardi)
#### Post date: [November 13, 2018, 1:52pm UTC](https://discourse.hibernate.org/t/how-to-soft-delete-a-child-entity-to-allow-the-client-to-review-it-before-it-finally-gets-deleted/1695/1 "2018-11-13T13:52:50Z")

</div>

Hi,

I have a process of Orders where I load Order and all their OrdemItem, and I’m having problems with this situation:

**Order: PK[id = 1];**

**OrderItem: PK[order\_id = 1, order\_line = 1];**  
**OrderItem: PK[order\_id = 1, order\_line = 2];**

So I remove the Object (**OrderItem: PK[order\_id = 1, order\_line = 1]**), only in collection because I don’t want to delete in database before the user confirm the whole process. But for any reason the user add the same OrderItem again in the collection.

If I merge the father (Order) after that I’m getting:

```auto
A different object with the same identifier value was already associated with the session

```

If I remove the OrderItem in database when I remove it from the collection it works, but I want to keep it in the database because the user could abort the process.

Is there any elegant approach to do this with Hibernate? Or should I do a workaround with @Transient attribute or another collection?

Thanks in advance

---

<div class="post-metadata">

### Author: ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)
#### Post date: [November 13, 2018, 2:12pm UTC](https://discourse.hibernate.org/t/how-to-soft-delete-a-child-entity-to-allow-the-client-to-review-it-before-it-finally-gets-deleted/1695/2 "2018-11-13T14:12:08Z")

</div>

> [@guibernardi](#):
>
> If I merge the father (Order) after that I’m getting:

That doesn’t happen with `merge`, but with `Session#update` or `Session#saveOrUpdate`.

> If I remove the OrderItem in database when I remove it from the collection it works, but I want to keep it in the database because the user could abort the process.

You need to use a `state` property in this case which is an Enum and takes values like:

- VALID
- REMOVING
- REMOVED

This way you can allow the user to review it prior to finally deleting it or changing it to the `REMOVED` state.

> Is there any elegant approach to do this with Hibernate? Or should I do a workaround with @Transient attribute or another collection?

There’s always an elegant solution with JPA and Hibernate. In this case, you could use the Hibernate `@Filter` to dynamically load the `OrderItem` based on their `status`. For more details, check out the [User Guide](http://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#mapping-column-filter).

---

<div class="post-metadata">

### Author: ![guibernardi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/guibernardi/32/90_2.png) [@guibernardi](https://discourse.hibernate.org/u/guibernardi)
#### Post date: [November 13, 2018, 3:44pm UTC](https://discourse.hibernate.org/t/how-to-soft-delete-a-child-entity-to-allow-the-client-to-review-it-before-it-finally-gets-deleted/1695/3 "2018-11-13T15:44:36Z")

</div>

Thank you, I’ll check and use @Filter
