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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Produces
@Singleton
public H2Persistence h2Persistence() {
HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider();
EntityManagerFactory entityManagerFactory = hibernatePersistenceProvider
.createContainerEntityManagerFactory(<persistence unit info>, <proprerties>);
runH2FlywayMigration();
return new H2Persistence(entityManagerFactory);
}
</code>
<code>@Produces @Singleton public H2Persistence h2Persistence() { HibernatePersistenceProvider hibernatePersistenceProvider = new HibernatePersistenceProvider(); EntityManagerFactory entityManagerFactory = hibernatePersistenceProvider .createContainerEntityManagerFactory(<persistence unit info>, <proprerties>); runH2FlywayMigration(); return new H2Persistence(entityManagerFactory); } </code>
@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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Caused by: com.oracle.svm.hosted.substitute.DeletedElementException: Unsupported type org.hibernate.jpa.HibernatePersistenceProvider is reachable
</code>
<code>Caused by: com.oracle.svm.hosted.substitute.DeletedElementException: Unsupported type org.hibernate.jpa.HibernatePersistenceProvider is reachable </code>
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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.
</code>
<code>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. </code>
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?

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Produces
@Singleton
public H2Persistence h2Persistence(
@PersistenceUnit("h2" /* or whatever name you used in config */)
EntityManagerFactory entityManagerFactory) {
runH2FlywayMigration();
return new H2Persistence(entityManagerFactory);
}
</code>
<code>@Produces @Singleton public H2Persistence h2Persistence( @PersistenceUnit("h2" /* or whatever name you used in config */) EntityManagerFactory entityManagerFactory) { runH2FlywayMigration(); return new H2Persistence(entityManagerFactory); } </code>
@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:

  • Basic configuration
  • Multiple EntityManagerFactories (since you want one for postgres and one for H2)
  • CDI integration

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật