# Hibernate soft delete setting foreign key relationship to null

**URL:** <https://discourse.hibernate.org/t/hibernate-soft-delete-setting-foreign-key-relationship-to-null/11927>\
**Category:** Hibernate ORM\
**Created:** [October 24, 2025, 3:16pm UTC](https://discourse.hibernate.org/t/hibernate-soft-delete-setting-foreign-key-relationship-to-null/11927 "2025-10-24T15:16:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamborgor](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@hamborgor](https://discourse.hibernate.org/u/hamborgor)\
**Post date:** [October 24, 2025, 3:16pm UTC](https://discourse.hibernate.org/t/hibernate-soft-delete-setting-foreign-key-relationship-to-null/11927/1 "2025-10-24T15:16:05Z")

</div>

When using the soft delete annotation, i believe that if I delete a entry that has a foreign key, the softdelete will set that to null. Is this suppose to be the case? I thought soft delete was just suppose to update the column deleted to 1. If it is, is there any way to get around this. I am using it for a one to one relationship. I tried using native sql queries and they return null as well

---

<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:** [October 27, 2025, 9:38am UTC](https://discourse.hibernate.org/t/hibernate-soft-delete-setting-foreign-key-relationship-to-null/11927/2 "2025-10-27T09:38:01Z")

</div>

Soft-delete will set a flag and requires that associations pointing to the soft-deleted entity are removed. This requirement is only enforced for entities pointing to the soft-deleted entity within the persistence context though, so you could end up with a dangling foreign key reference, but Hibernate ORM will ensure you can’t “observe” the actual object, by applying a deleted flag filter when the association is fetched.

So yes, what you observe is somewhat expected.

---

<div class="post-metadata">

**Author:** ![hamborgor](https://avatars.discourse-cdn.com/v4/letter/h/b77776/32.png) [@hamborgor](https://discourse.hibernate.org/u/hamborgor)\
**Post date:** [October 27, 2025, 1:06pm UTC](https://discourse.hibernate.org/t/hibernate-soft-delete-setting-foreign-key-relationship-to-null/11927/3 "2025-10-27T13:06:13Z")

</div>

Thank you so much for the reply.

Is there any way to get the dangling foreign key references? I’ve seen people say to use native SQL Queries, but I tried that and it didn’t work for me. However, I could be doing it wrong.

---

<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:** [October 27, 2025, 4:17pm UTC](https://discourse.hibernate.org/t/hibernate-soft-delete-setting-foreign-key-relationship-to-null/11927/4 "2025-10-27T16:17:53Z")

</div>

There is no way to access soft-deleted entities through Hibernate ORM means, since the filters for soft-delete is always applied to all HQL/Criteria queries. You would have to use a native query to access the soft-deleted entity data.  
Theoretically, you could access the FK value of an association directly with the [`fk()` function](https://docs.hibernate.org/orm/7.2/userguide/html_single/#hql-functions-model).
