# OneToMany update. Delete always after insert in Hibernate

**URL:** <https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444>\
**Category:** Hibernate ORM\
**Created:** [June 18, 2021, 7:53am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444 "2021-06-18T07:53:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![KenNexify](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@KenNexify](https://discourse.hibernate.org/u/KenNexify)\
**Post date:** [June 18, 2021, 7:53am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/1 "2021-06-18T07:53:50Z")

</div>

I have 3 tables with 3-level OneToMany relationships.

 ![Untitled Diagram](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/3/34c41129c0574942e46672f93b42bcc135626870.png)

Now I am trying to delete one record in table B and re-insert a new record with the same name, same parent, and new table C records in one action using hibernate. The duplicate key error is thrown because of Hibernate insert first behavior. I have already read this [post](https://forum.hibernate.org/viewtopic.php?t=934483) so I think I may be misused hibernate.

What is the best practice for my case?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 21, 2021, 12:55pm UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/2 "2021-06-21T12:55:55Z")

</div>

Can’t you just flush the persistence context before doing the insert by using `EntityManager.flush`/`Session.flush`?

---

<div class="post-metadata">

**Author:** ![KenNexify](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@KenNexify](https://discourse.hibernate.org/u/KenNexify)\
**Post date:** [June 22, 2021, 2:43am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/3 "2021-06-22T02:43:06Z")

</div>

I have not to try to do it but I think I can flush before insert. I just confuse because I get the message from the post: “usually wanting to delete then reinsert is a sign that you are trying to use Hibernate in the wrong way.”

So should I consider that is a special case so Hibernate without querying is not suitable to handle this case?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 22, 2021, 6:21am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/4 "2021-06-22T06:21:27Z")

</div>

Well if you think about it, it’s unnecessary to delete the object in the first place if you are going to re-insert it later anyway. IMO you should just update the existing object rather then deleting and re-inserting.

---

<div class="post-metadata">

**Author:** ![KenNexify](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@KenNexify](https://discourse.hibernate.org/u/KenNexify)\
**Post date:** [June 22, 2021, 6:44am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/5 "2021-06-22T06:44:12Z")

</div>

Well… I am not trying to re-insert.

In my table structure, the unique key is preventing the same name appear in my system. For my system user, he just deletes an old record and creates a new record with the same name in one action. The new record in table B might be one-to-many the new table C records.

Sorry for my bad English presentation but I think it’s definitely a new record…

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 22, 2021, 3:02pm UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/6 "2021-06-22T15:02:55Z")

</div>

I undestand that, but in your code, rather than doing `oneToMany.remove(..)` and `oneToMany.add(...)` you should be doing something like this `oneToMany.stream().filter(..).findFirst().orElse(() -> {T elem = new T(); oneToMany.add(elem); return elem; })` which will retain the same object in the one-to-many association.

---

<div class="post-metadata">

**Author:** ![KenNexify](https://avatars.discourse-cdn.com/v4/letter/k/f4b2a3/32.png) [@KenNexify](https://discourse.hibernate.org/u/KenNexify)\
**Post date:** [June 23, 2021, 3:12am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/7 "2021-06-23T03:12:08Z")

</div>

Thanks, I think that is a solution for technical only. However, I think it is not the best solution because it is mismatched the user’s action in my DB. I mean the user action is definitely deleted one and then create a new one, but we are trying to replace the old one. Anyway thanks beikov.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [June 24, 2021, 7:18am UTC](https://discourse.hibernate.org/t/onetomany-update-delete-always-after-insert-in-hibernate/5444/8 "2021-06-24T07:18:53Z")

</div>

I agree that Hibernate could try to do better here, but it is what it is right now. You’re welcome
