hypersistence-optimizer icon indicating copy to clipboard operation
hypersistence-optimizer copied to clipboard

[QUARKUS 3] The Hibernate SessionFactory is not decorated by Hypersistence Optimizer, and, for this reason, the runtime checks will be disabled

Open Vinche59 opened this issue 2 years ago • 9 comments

Hello,

With Quarkus 3.0.3 (Hibernate 6.2.1.Final + postgresql), I always got:

The Hibernate SessionFactory is not decorated by Hypersistence Optimizer, and, for this reason, the runtime checks will be disabled.

And I don´t understand what I'm doing wrong :-)

I created a reproducer here : https://github.com/Vinche59/hypersistence-optimizer-quarkus3.0.3

You can run it with: mvn integration-test

Thanks in advance for your help.

Vinche59 avatar May 23 '23 15:05 Vinche59

Thanks for letting me know. I will investigate it and see what's needed to be done to make it work.

vladmihalcea avatar May 23 '23 16:05 vladmihalcea

@Vinche59 Unfortunately, runtime scanning cannot work for Quarkus because they are not using the Hibernate EntityManagerFactoryBuilderImpl to build the EntityManagerFactory, which would allow Hypersistence Optimizer to decorate the SessionFactory via its own SessionFactoryBuilder.

Instead, they created their own FastBootEntityManagerFactoryBuilder that builds the SessionFactory like this:

public EntityManagerFactory build() {
	try {
		SessionFactoryOptionsBuilder optionsBuilder = this.metadata.buildSessionFactoryOptionsBuilder();
		this.populate(this.persistenceUnitName, optionsBuilder, this.standardServiceRegistry);
		return new SessionFactoryImpl(this.metadata, optionsBuilder.buildOptions(), this.metadata.getTypeConfiguration().getMetadataBuildingContext().getBootstrapContext());
	} catch (Exception var2) {
		throw this.persistenceException("Unable to build Hibernate SessionFactory", var2);
	}
}

Notice that the SessionFactoryImpl is hardcoded and cannot be decorated by the SessionFactoryBuilder.

On the other hand, this works fine with Spring or plain Jakarta EE apps.

For instance, the Spring HibernatePersistenceProvider builds the SessionFactory, like this:

public static EntityManagerFactoryBuilder getEntityManagerFactoryBuilder(
		PersistenceUnitDescriptor persistenceUnitDescriptor,
		Map integration,
		ClassLoader providedClassLoader) {
	return new EntityManagerFactoryBuilderImpl( persistenceUnitDescriptor, integration, providedClassLoader );
}

So, the only way to make this work is if Quarkus switches to using something similar to the Hibernate Core EntityManagerFactoryBuilder.

vladmihalcea avatar May 23 '23 17:05 vladmihalcea

I created this Quarkus issue. If they fix it, the Hypersistence Optimizer will be able to declare the SessionFactory and runtime scanning will be possible.

vladmihalcea avatar May 23 '23 17:05 vladmihalcea

Thank you for your investigation @vladmihalcea and for opening the quarkus issue.

Vinche59 avatar May 23 '23 17:05 Vinche59

You're welcome.

vladmihalcea avatar May 23 '23 17:05 vladmihalcea

Hello @vladmihalcea https://github.com/quarkusio/quarkus/issues/33564#issuecomment-2396475565 There is an answer to a topic. Not sure if you would be considering contribution.

adampoplawski avatar Oct 08 '24 11:10 adampoplawski

@adampoplawski, At the moment, I don't have the time to contribute that change to Quarkus, but if you have the time, you can surely provide the PR that does what Yoann suggested. Looking forward to your contribution.

vladmihalcea avatar Oct 08 '24 11:10 vladmihalcea

hello @vladmihalcea Quarkus upgraded to hibernate orm 7 + reactive 3. I assume this is not helping this case and we still need 3rd party fix?

adampoplawski avatar Jul 09 '25 15:07 adampoplawski

@adampoplawski Give it a try and see whether the problem got fixed.

vladmihalcea avatar Jul 09 '25 19:07 vladmihalcea