8000 Migrate default persistence unit to Quarkus Hibernate ORM properties by michalvavrik · Pull Request #52213 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Migrate default persistence unit to Quarkus Hibernate ORM properties - #52213

Merged
vmuzikar merged 14 commits into
keycloak:mainfrom
michalvavrik:in-review/51121
Sep 24, 2026
Merged

vmuzikar merged 14 commits into
keycloak:mainfrom
michalvavrik:in-review/51121

Conversation

@michalvavrik
Copy link
Copy Markdown
Member

Copilot AI balanced review requested due to automatic review settings August 30, 2026 23:48
@michalvavrik
michalvavrik requested review from a team as code owners August 30, 2026 23:48
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Migrates Keycloak’s default persistence unit from legacy persistence.xml configuration to Quarkus Hibernate ORM properties while retaining legacy test coverage and custom persistence units.

Changes:

  • Configures the default persistence unit through Quarkus properties and listeners.
  • Reconstructs user-defined persistence units from persistence.xml.
  • Adds integration tests for properties, dialects, entities, and inactive datasources.

Reviewed changes

Copilot reviewed 11 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
testsuite/model/.../JpaConnectionProviderFactory Registers the test-only JPA provider.
testsuite/model/.../default-persistence.xml Adds legacy persistence configuration for model tests.
testsuite/model/.../DefaultJpaConnectionProviderFactory.java Adds the test-only legacy JPA factory.
tests/base/.../DefaultPuPropertiesTest.java Tests default PU properties and entities.
quarkus/tests/.../CustomJpaEntityProviderDistTest.java Updates custom-PU integration assertions.
quarkus/tests/.../persistence.xml Uses a test-specific Hibernate dialect.
quarkus/tests/.../KeycloakItH2Dialect.java Adds the test dialect implementation.
quarkus/tests/.../CustomJpaEntityProvider.java Packages the test dialect.
quarkus/runtime/.../KeycloakRecorderTest.java Tests schema-action restoration.
quarkus/runtime/.../application.properties Adds default Hibernate ORM properties.
quarkus/runtime/.../QuarkusJpaConnectionProviderFactory.java Resolves the unnamed default EMF directly.
quarkus/runtime/.../NamedJpaConnectionProviderFactory.java Handles inactive named datasources.
quarkus/runtime/.../KeycloakRecorder.java Adds static/runtime Hibernate listeners.
quarkus/deployment/.../PersistenceXmlDatasourcesTest.java Tests persistence-unit conversion helpers.
quarkus/deployment/.../KeycloakProcessor.java Produces and configures Quarkus persistence units.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 16 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings August 31, 2026 10:50
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 16 changed files in this pull request and generated 1 comment.

@keycloak-github-bot
Copy link
Copy Markdown

Unreported flaky test detected

If the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR.

org.keycloak.testsuite.actions.TermsAndConditionsTest#termsAccepted

Keycloak CI - Forms IT (firefox)

java.lang.RuntimeException: Could not create statement
	at org.jboss.arquillian.junit.Arquillian.methodBlock(Arquillian.java:307)
	at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:160)
	at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:344)
	at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:48)
...

Report flaky test

@keycloak-github-bot keycloak-github-bot Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 8000 reason will be displayed to describe this comment to others. Learn more.

Unreported flaky test detected, please review

@shawkins shawkins left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks very good. It gets us off of the quarkus persistence.xml mechanism, but retains nominal compatibility - would quarkus be interested in directly supporting this bridge?

Also where DefaultJpaConnectionProviderFactory was moved to looks at first glance like it won't work for manual launches of the org.keycloak.testsuite.KeycloakServer undertow based server - which some devs still seem to be doing.

@michalvavrik
Copy link
Copy Markdown
Member Author

would quarkus be interested in directly supporting this bridge?

my interpretation of chat I just had with @yrodiere is "not really" :-)

Also where DefaultJpaConnectionProviderFactory was moved to looks at first glance like it won't work for manual launches of the org.keycloak.testsuite.KeycloakServer undertow based server - which some devs still seem to be doing.

That is just a legacy test suite that will be removed, correct? I haven't seen it before you commented as it doesn't seem to be part of any tests, but running mvavrik@fedora:~/sources/keycloak/testsuite/utils$ mvn exec:java -Pkeycloak-server showed error, I'll look into it and try to provide a fix.

@vmuzikar vmuzikar left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalvavrik Thanks, nice work.

Copilot AI review requested due to automatic review settings September 4, 2026 00:21
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

User persistence units can lose implicit ORM mappings and unlisted entities, while warning suppression can hide user-provided unsupported settings.

Review details

Suppressed comments (3)

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java:581

  • An implicit META-INF/orm.xml is valid even when the persistence unit has no <class> entries; this branch skips it and turns an XML-only persistence unit into an empty unit. Always probe the implicit mapping and use no-file only when the parser confirms it is absent.
            if (!descriptor.getManagedClassNames().isEmpty()) {
                mappingFiles.add("META-INF/orm.xml");
            } else {
                builder.mappingFile("no-file");
                return;

quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/logging/KeycloakLogFilter.java:85

  • This whitelist contains every possible mapper destination, not only properties actually contributed under the current configuration. For example, a user who directly sets hibernate.use_sql_comments while db-debug-jpql is disabled will have Quarkus' unsupported-property warning incorrectly suppressed; derive the set from active Keycloak mappings/defaults instead.
    private static Set<String> collectAllDefaultUnsupportedHibernateProperties() {
        Stream<String> properties = new DatabasePropertyMappers().getPropertyMappers().stream().map(PropertyMapper::getTo);

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java:541

  • This drops exclude-unlisted-classes=false semantics: only explicit <class> and XML-mapped types are assigned to the named unit, so annotated classes elsewhere in the persistence-unit root (such as the new UnlistedEntity) can instead be auto-discovered into the default unit. Preserve the descriptor's unlisted-class behavior by discovering and assigning those root classes to this build item (and excluding them from the default unit).
            for (String className : descriptor.getManagedClassNames()) {
                builder.managedClass(className);
            }
            for (String mappingFile : descriptor.getMappingFileNames()) {
                builder.mappingFile(mappingFile);
  • Files reviewed: 22/25 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The migration omits hibernate.order_updates and ignores implicit orm.xml for units without explicit managed classes.

Review details

Suppressed comments (2)

quarkus/runtime/src/main/resources/application.properties:22

  • hibernate.order_updates=true from the former default persistence unit was not migrated here. Hibernate's default is false, so this changes update batching behavior and makes DefaultPuPropertiesTest.hibernatePropertiesSurvival() fail; add the corresponding unsupported property alongside order_inserts.
quarkus.hibernate-orm.unsupported-properties."hibernate.order_inserts"=true

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java:581

  • A valid persistence unit may define all of its entities in the implicit META-INF/orm.xml and have no <class> entries. This early return silently replaces that mapping with no-file; always probe the implicit mapping and let the existing anyMappingFound fallback handle the absent-file case.
            if (!descriptor.getManagedClassNames().isEmpty()) {
                mappingFiles.add("META-INF/orm.xml");
            } else {
                builder.mappingFile("no-file");
                return;
  • Files reviewed: 22/25 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Pepo48
Pepo48 previously approved these changes Sep 17, 2026
@shawkins
Copy link
Copy Markdown
Contributor

@michalvavrik your comment on the handling of the default orm.xml makes sense for non-default persistence units.

I don't think https://github.com/keycloak/keycloak/pull/52213/changes#r4028896253 was fully answered - will the default orm.xml be used with the default persistence unit with these changes? Based upon the linked issue it seems like that was expected at some point.

@michalvavrik
Copy link
Copy Markdown
Member Author

@michalvavrik your comment on the handling of the default orm.xml makes sense for non-default persistence units.

I don't think https://github.com/keycloak/keycloak/pull/52213/changes#r4028896253 was fully answered - will the default orm.xml be used with the default persistence unit with these changes? Based upon the linked issue it seems like that was expected at some point.

I didn't think users should do that, it wasn't documented anywhere, or tested. Let's "support it" then. I reworked it yesterday and added some tests. TBH I was going in a circles as there was what I'd call a dirty trick in Quarkus Hibernate code (sorry for saying that) that allowed them to avoid some build-time validations we cannot avoid, so it wasn't an issue when otherwise empty PU with only entities mapped from orm.xml turned out to point to JDBC URL that was unreachable. I am not sure that empty PUs are realistic scenarios for us anyway, but since I try to keep everything without breaking changes, I need to consider that.

I need to re-read all related code now that I am not tired and if what I have now is 1-to-1 with previous orm.xml related behavior, hopefully with next push we can close this one.

@shawkins
Copy link
Copy Markdown
Contributor

I need to re-read all related code now that I am not tired and if what I have now is 1-to-1 with previous orm.xml related behavior, hopefully with next push we can close this one.

Sounds good. Thank you for your patience in resolving this without breaking anyone. It would have been better if we made definitive statements about what we supported to make this easier.

closes: keycloak#51121

Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
< 10BC8 a class="author Link--primary text-bold css-overflow-wrap-anywhere" data-hovercard-type="copilot" data-hovercard-url="/copilot/hovercard?bot=copilot-pull-request-reviewer" href="/apps/copilot-pull-request-reviewer">Copilot AI reviewed Sep 18, 2026
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

User persistence units lose standard JPA support for referenced JARs, unlisted entities, and implicit ORM mappings.

Review details

Suppressed comments (3)

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java:537

  • This drops standard <jar-file> support for user persistence units, so existing providers that keep entities in referenced JARs will silently boot without those entities after upgrading. That conflicts with the linked issue's requirement of no functional impact; preserve the referenced roots in the generated persistence-unit contributions instead of only warning.
            if (descriptor.getJarFileUrls() != null && !descriptor.getJarFileUrls().isEmpty()) {
                logger.warnf("Persistence unit '%s' declares <jar-file> (%s), which is not supported; entities from a referenced jar are not added to this unit."
                        + " List them with <class> or package them in the unit's own jar.", puName, descriptor.getJarFileUrls());

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java:545

  • Only explicitly listed classes are transferred, while exclude-unlisted-classes and persistence-unit-root scanning are ignored. A valid user unit that relies on the default/false setting to discover annotated entities will therefore lose those entities (or have them assigned to the default unit), which is a behavior regression from native persistence.xml processing.
            AdditionalPersistenceUnitBuildItem.Builder builder = AdditionalPersistenceUnitBuildItem.builder(puName)
                    .dataSourceName(datasourceName);
            for (String className : descriptor.getManagedClassNames()) {
                builder.managedClass(className);
            }

quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/KeycloakProcessor.java:587

  • The absence of <class> entries is used to suppress implicit META-INF/orm.xml, but JPA loads that default mapping whenever no <mapping-file> is declared. Consequently, a valid persistence unit whose entities are defined solely in META-INF/orm.xml stops working; the linked issue explicitly requires no functional impact, so always probe the implicit mapping and use no-file only when it is absent.
            if (!descriptor.getManagedClassNames().isEmpty()) {
                mappingFiles.add("META-INF/orm.xml");
            } else {
                builder.mappingFile("no-file");
                return;
  • Files reviewed: 26/30 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@michalvavrik
Copy link
Copy Markdown
Member Author

I'll provide one more small change.

Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 30 out of 35 changed files in this pull request and generated 4 comments.

Suppressed comments (1)

tests/base/src/test/java/org/keycloak/tests/db/DefaultPuPropertiesTest.java:1

  • This assertion is brittle: the entity count can legitimately change as entities are added/removed/refactored, causing unrelated failures. Prefer asserting on a smaller set of representative core entities (which you already do below) and/or asserting that the count is greater than zero / above a very conservative threshold tied to an invariant.

@michalvavrik
Copy link
Copy Markdown
Member Author

@shawkins whenever you are ready, this can be reviewed.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Copilot reviewed 30 out of 35 changed files in this pull request and generated 3 comments.

@michalvavrik
Copy link
Copy Markdown
Member Author

On slightly related note, I created #53016 that should be done probably ASAP so that someone doing migrations of a legacy test suite is not confused.

@shawkins shawkins left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @michalvavrik LGTM, I don't any other concerns.

@vmuzikar do you want to take a final look?

@vmuzikar vmuzikar left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it one final quick look (relying on reviews of the recent changes from others), LGTM. Thanks.

configureDefaultPersistenceUnitProperties(descriptor, config, getDefaultDataSource(jdbcDataSources));
runtimeConfigured.produce(new HibernateOrmIntegrationRuntimeConfiguredBuildItem("keycloak", descriptor.getName())
.setInitListener(recorder.createDefaultUnitListener()));
private static void enlistMappingFileEntities(AdditionalPersistenceUnitBuildItem.Builder builder, PersistenceUnitDescriptor descriptor, boolean isSolePersistenceUnitInRoot) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an observation, let's keep it as is for now...

The logic here gets really complex, therefore hard to maintain. We really need to reevaluate if we can't get rid of the custom prestistence.xml support.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are other ways how to simplify it. A lot of the logic in processor and recorder is to avoid breaking changes for things that noone may be doing or can do differently. It is a gray area. I can we could as well state in 27.0 we support A, B, C and if you need something else, use Quarkus Hibernate properties and other standard means supported by Hibernate.

I am pretty sure we will have similar discussion in my next PR, so we can continue there instead of here. I'll open it by the end of this week.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you already asked to create issue that removes support for the custom persistence xml, so maybe there is no discussion needed anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate default persistence.xml to Quarkus Hibernate properties

6 participants

0