Search code examples
hibernatequarkusquarkus-nativequarkus-hibernate-orm

Using HibernatePersistenceProvider in Quarkus compiled into native image


I have a legacy piece of Quarkus code that i'm trying to force to work in native mode. It uses the following configuration to create an entity manager to work with embedded H2

@Produces
@Singleton
public H2Persistence h2Persistence() {
    HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider();
    EntityManagerFactory entityManagerFactory = hibernatePersistenceProvider
            .createContainerEntityManagerFactory(<persistence unit info>, <proprerties>);
    runH2FlywayMigration();
    return new H2Persistence(entityManagerFactory);
}

Main DB is postgres, H2 is just sort of a local cache, to speed things up and ensure objects can still be accessed for reads if database is down.
It looks like this particular provider is not supported for native builds, however, as i'm getting an error on compilation

Caused by: com.oracle.svm.hosted.substitute.DeletedElementException: Unsupported type org.hibernate.jpa.HibernatePersistenceProvider is reachable

I can see a rather suspicious note in quarkus' FastBootHibernatePersistenceProvider

This can not inherit from HibernatePersistenceProvider as that would force the native-image tool to include all code which could be triggered from using that: we need to be able to fully exclude HibernatePersistenceProvider from the native image.

Makes me believe that HibernatePersistenceProvider breaks something in native build and so it was disabled in Quarkus. However, FastBootHibernatePersistenceProvider doesn't look like something i'd want to manually instantiate as its constructor parameters aren't exactly manual-config friendly.
Is there any sensible substitution to HibernatePersistenceProvider that would work in native or should i just throw this old code and rewrite the whole thing?


Solution

  • You should not try to create your EntityManagerFactory manually in Quarkus. That's not expected to work -- especially in native mode.

    Assuming you configured your persistence unit in application.properties, the EntityManager is already created by Quarkus and available in CDI, just use that:

    @Produces
    @Singleton
    public H2Persistence h2Persistence(
                @PersistenceUnit("h2" /* or whatever name you used in config */)
                EntityManagerFactory entityManagerFactory) {
        runH2FlywayMigration();
        return new H2Persistence(entityManagerFactory);
    }
    

    If you need to pass properties to the factory, use application.properties and quarkus.hibernate-orm.*.

    See also: