# Degradation of performance after migration Hibernate 6

**URL:** <https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733>\
**Category:** Hibernate ORM\
**Created:** [August 14, 2025, 9:12am UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733 "2025-08-14T09:12:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MHA](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@MHA](https://discourse.hibernate.org/u/MHA)\
**Post date:** [August 14, 2025, 9:12am UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733/1 "2025-08-14T09:12:39Z")

</div>

After migrating from hibernate 5.6.15 (Spring boot 2) to hibernate 6.6.25 (Spring boot 3) I have a degradation of performance in the application here are the metrics between the two versions when I do a saveAll(entities):

Hibernate 5.6.15

> Session Metrics {
> 
> ```
> 7409705 nanoseconds spent acquiring 1 JDBC connections;
> 
> 0 nanoseconds spent releasing 0 JDBC connections;
> 
> 43892924 nanoseconds spent preparing 4231 JDBC statements;
> 
> 2427394924 nanoseconds spent executing 3117 JDBC statements;
> 
> 2407533368 nanoseconds spent executing 1122 JDBC batches;
> 
> 0 nanoseconds spent performing 0 L2C puts;
> 
> 0 nanoseconds spent performing 0 L2C hits;
> 
> 0 nanoseconds spent performing 0 L2C misses;
> 
> 5073676795 nanoseconds spent executing 53 flushes (flushing a total of 193239 entities and 527360 collections);
> 
> 8123499 nanoseconds spent executing 7 partial-flushes (flushing a total of 2915 entities and 2915 collections)
> 
> ```
> 
> }

Hibernate 6.6.25

> Session Metrics {
> 
> ```
> 10729076 nanoseconds spent acquiring 1 JDBC connections;
> 
> 0 nanoseconds spent releasing 0 JDBC connections;
> 
> 4991779002 nanoseconds spent preparing 523347 JDBC statements;
> 
> 442802734144 nanoseconds spent executing 522229 JDBC statements;
> 
> 2675328028 nanoseconds spent executing 1351 JDBC batches;
> 
> 0 nanoseconds spent performing 0 L2C puts;
> 
> 0 nanoseconds spent performing 0 L2C hits;
> 
> 0 nanoseconds spent performing 0 L2C misses;
> 
> 43348625619 nanoseconds spent executing 53 flushes (flushing a total of 2389057 entities and 9493723 collections);
> 
> 7845319 nanoseconds spent executing 3 pre-partial-flushes;
> 
> 25265613 nanoseconds spent executing 9 partial-flushes (flushing a total of 4063 entities and 4063 collections)
> 
> ```
> 
> }

Thank you!

---

<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:** [August 14, 2025, 9:48am UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733/2 "2025-08-14T09:48:49Z")

</div>

This report is not really useful. Clearly your new application version takes a lot of time to acquire JDBC connection and also processes way more data, so clearly, there will be a difference in execution time.  
If you want help, you will have to analyze this situation yourself a bit further and ask more targeted questions.

---

<div class="post-metadata">

**Author:** ![MHA](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@MHA](https://discourse.hibernate.org/u/MHA)\
**Post date:** [August 14, 2025, 12:54pm UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733/3 "2025-08-14T12:54:38Z")

</div>

Thank you for your reply. I ran the same scenario and had the same amount of data to save (entities).

> total ShiftNameEntity : 123  
> total VariantEntity : 0  
> total ShiftPartEntity : 293  
> total ServiceCounterEntity : 0

Here’s my code:

`serviceEntityDao.saveAll(services);`

I used a for loop to see the number of queries executed for each entity. I noticed that with Hibernate version 6, it executes more queries than with version 5.

> for(ServiceEntity serviceEntity : services) {  
> serviceEntityDao.save(serviceEntity);  
> }

---

<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:** [August 14, 2025, 2:54pm UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733/4 "2025-08-14T14:54:30Z")

</div>

I don’t know what this `saveAll` method does. Please, this is a Hibernate ORM forum, so reduce the code to the Hibernate ORM primitives like `EntityManager#persist`/`EntityManager#merge`.

---

<div class="post-metadata">

**Author:** ![MHA](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@MHA](https://discourse.hibernate.org/u/MHA)\
**Post date:** [August 14, 2025, 8:41pm UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733/5 "2025-08-14T20:41:25Z")

</div>

I finally identified the problem. Here are my two JPA entities:

```java
Public class FirstEntity {
…
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "name", referencedColumnName = "name")
private SecondEntity firstEntity;
…
}

Public class SecondEntity {
…
@OneToMany(cascade = CascadeType.ALL, mappedBy = "firstEntity")
private List< FirstEntity > firstEntities;
…
}

```

The multiple queries were triggered because I declared the @OneToManyOneToMany relationship in SecondEntity.

What I don’t understand is why Hibernate 5 didn’t report this issue, but with Hibernate 6, it triggers additional queries.

After remo@OneToManying the @OneToMany relationship in SecondEntity, I no longer experienced any performance issues and the number of queries executed significantly decreased.

Thank you for your help.

---

<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:** [August 15, 2025, 10:04am UTC](https://discourse.hibernate.org/t/degradation-of-performance-after-migration-hibernate-6/11733/6 "2025-08-15T10:04:56Z")

</div>

I have no idea what you think you’re doing, but removing `@OneToMany` is probably not a solution. I suppose that your or the Spring code uses `EntityManager#merge` under the hood, which will trigger joining of of associations that are `CascadeType.MERGE` or `CascadeType.ALL` to get the initial state for that `merge` call via a select statement.  
If you’re trying to insert data, consider using `EntityManager#persist`. If you are in fact updating existing data, you have two options. If you can change the `CascadeType`, then do that. Otherwise, try loading the existing data manually first and apply the changes to these objects directly instead of using `EntityManager#merge`. That way, you control what data is loaded explicitly and only the relevant data is flushed.
