Hibernate v5->v6 upgrade - poor query performance

I’ve upgraded an application using Hibernate v5 to v6, but after doing this a query has got very slow - >10x slower.

Take the following simple application that persists 500,000 MyEntitys to a new in-memory database, retrieves them and prints performance metrics. It can be run with either Hibernate v5 or v6, as per the commented out section in pom.xml:

MyApplication.java:

package com.me;

import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Properties;
import java.util.stream.IntStream;

import org.h2.Driver;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.schema.Action;

public class MyApplication {
    public static void main(final String[] args) {
	Instant start = Instant.now();

	final Properties jpaProperties = new Properties();

	jpaProperties.put("hibernate.connection.url", "jdbc:h2:mem:");
	jpaProperties.put("jakarta.persistence.jdbc.driver", Driver.class.getName());
	jpaProperties.put("jakarta.persistence.schema-generation.database.action", Action.CREATE);

	try (Session session = new Configuration().addAnnotatedClass(MyEntity.class).addProperties(jpaProperties)
		.buildSessionFactory().openSession()) {
	    session.beginTransaction();
	    IntStream.range(0, 500000).mapToObj(i -> new MyEntity()).forEach(session::persist);
	    printTiming(start, "Setup / Publish");

	    start = Instant.now();
	    session.createQuery("FROM MyEntity", MyEntity.class).getResultList();
	    printTiming(start, "Get");
	}
    }

    private static void printTiming(final Instant startTime, final String label) {
	System.out.println(MessageFormat.format("{0} took {1}", label, Duration.between(startTime, Instant.now())));
    }
}

MyEntity.java:

package com.me;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<groupId>com.me</groupId>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>hibernate-test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>2.1.214</version>
		</dependency>
		<dependency>
			<groupId>jakarta.xml.bind</groupId>
			<artifactId>jakarta.xml.bind-api</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate.orm</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>6.1.5.Final</version>
		</dependency>
		<!--<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core-jakarta</artifactId>
			<version>5.6.14.Final</version>
		</dependency>-->
	</dependencies>
</project>

hibernate-core 6.1.5:

Setup / Publish took PT2.6288547S
Get took PT35.0881315S

hibernate-core-jakarta 5.6.14.Final:

Setup / Publish took PT3.486003S
Get took PT2.3955987S

As per java - Hibernate v5->v6 upgrade - poor SELECT performance - Stack Overflow, it looks to be related to a resolved, but yet to be released, bugs:

You are right, there were some issues that have now been fixed. Version 6.1.6 will be released this week, then you can tell us your updated numbers.

Just benchmarked it again in Hibernate v6.1.5 @ 37 seconds, v6.1.6 @ 3 seconds.