# orphanRemoval=true triggers loading of never-touched lazy @OneToMany collections on save() of a detached entity (Hibernate 5 → 6.6.53 regression?)

**URL:** <https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416>\
**Category:** Hibernate ORM\
**Created:** [September 4, 2026, 7:54am UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416 "2026-09-04T07:54:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_G2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sebastian_g2/32/4383_2.png) [@Sebastian\_G2](https://discourse.hibernate.org/u/Sebastian_G2)\
**Post date:** [September 4, 2026, 7:54am UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416/1 "2026-09-04T07:54:42Z")

</div>

Hi everyone,

I’m running into unexpected behavior after migrating from Hibernate 5 to Hibernate 6.6.53.Final as part of a Spring Boot 2.7 → 3.x migration.

**Setup**

I have a parent entity with several `@OneToMany` collections mapped like this:

```auto
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<Child> children = new ArrayList<>();

```

There are multiple such lazy collections on the same entity.

**Steps to reproduce**

1. Load the parent entity in one transaction (the lazy collections are _not_ accessed/initialized at this point).
2. The entity becomes detached (e.g. transaction/session ends).
3. In a later transaction, `save()`/`merge()` the detached parent entity — without ever touching any of the collections, and without having changed anything in them.

**Expected behavior (and actual behavior under Hibernate 5)**

Since the collections were never initialized and therefore cannot have been modified, I would expect Hibernate to skip them entirely during flush — no additional SELECTs. This is exactly what happened under Hibernate 5.

**Actual behavior under Hibernate 6.6.53.Final**

When saving the parent, Hibernate now issues SELECT queries to load every one of these lazy collections, even though they were never accessed. This happens for all of them, not just one, which multiplies the number of unnecessary queries significantly on entities with several such mappings.

**Questions**

1. Is this a known/intended behavior change between Hibernate 5 and 6, related to how `orphanRemoval` snapshot comparison now works during flush?
2. Is this considered a bug, or is there a design reason why an uninitialized collection with `orphanRemoval=true` must now be loaded on every save?
3. Is there a recommended way to avoid this — short of dropping `orphanRemoval` and handling child deletion explicitly via a separate repository call?

I’m happy to put together a minimal reproducer using the test case template if that would help pin this down. Thanks in advance for any insight!

---

<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:** [September 4, 2026, 8:54am UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416/2 "2026-09-04T08:54:26Z")

</div>

I wouldn’t say that it is intentional to run N queries for every collection. There should be a single join fetch instead, but we don’t yet detect that this kind of “bag” actually has unique elements, which is why we have to use separate select statements for now.  
Does the behavior change if you remove `orphanRemoval`?  
We do have an intentional behavior change regarding how cascades are processed, but that shouldn’t really make a difference in your case.

Please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-7/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.

---

<div class="post-metadata">

**Author:** ![Sebastian\_G2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sebastian_g2/32/4383_2.png) [@Sebastian\_G2](https://discourse.hibernate.org/u/Sebastian_G2)\
**Post date:** [September 4, 2026, 9:10am UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416/3 "2026-09-04T09:10:02Z")

</div>

Just for clarification it will not run N queries for each child collection, but only one for each collection.  
So if i have for example 5 `@OneToMany` in the parent it will execute 1+5 queries during save of the parent.  
But the collection are lazy and not loaded.  
Hibernate 5 uses only one query here during merge.

And no, it does not change when i remove the `orphanRemoval.`  
Only thing that canged this behavious is when i remove the `CascadeType.Merge`

---

<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:** [September 4, 2026, 12:58pm UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416/4 "2026-09-04T12:58:58Z")

</div>

Sounds like you are affected by [HHH-20394](https://hibernate.atlassian.net/browse/HHH-20394), which should be solved once you update to the latest version.

---

<div class="post-metadata">

**Author:** ![Sebastian\_G2](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/sebastian_g2/32/4383_2.png) [@Sebastian\_G2](https://discourse.hibernate.org/u/Sebastian_G2)\
**Post date:** [September 4, 2026, 3:37pm UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416/5 "2026-09-04T15:37:20Z")

</div>

I dont think that this describes my issue and i am already using Hibernate 6.6.53.

```auto
class RootEntity {
  @OneToMany(mappedBy="root", fetch=LAZY, cascade=ALL)
  Set<Child> children;
}

class Child{
  @ManyToOne(fetch=LAZY)
  RootEntity root;
}

```

In my case it looks like this.  
Now i am loading the Root entity without fetching the children. So the set is not initialized.  
When i now call merge on the root entity, it will load all the children. That means it will cascade the merge operation to the child collection even the set is not initialized.

---

<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:** [September 7, 2026, 11:00am UTC](https://discourse.hibernate.org/t/orphanremoval-true-triggers-loading-of-never-touched-lazy-onetomany-collections-on-save-of-a-detached-entity-hibernate-5-6-6-53-regression/12416/6 "2026-09-07T11:00:26Z")

</div>

AFAIU, this is exactly what HHH-20394 is about, but if you think you found another problem, please try to create a reproducer with our [test case template](https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-7/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) and if you are able to reproduce the issue, create a bug ticket in our [issue tracker](https://hibernate.atlassian.net) and attach that reproducer.
