Repository navigation
Migrate default persistence unit to Quarkus Hibernate ORM properties - #52213
Conversation
There was a problem hiding this comment.
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.
468a4bd to
e189fd9
Compare
e189fd9 to
e76ff41
Compare
Unreported flaky test detectedIf 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#termsAcceptedKeycloak CI - Forms IT (firefox) |
There was a problem hiding this comment.
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.
my interpretation of chat I just had with @yrodiere is "not really" :-)
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 |
There was a problem hiding this comment.
@michalvavrik Thanks, nice work.
e76ff41 to
6bf3dbe
Compare
There was a problem hiding this comment.
🔵 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.xmlis 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 useno-fileonly 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_commentswhiledb-debug-jpqlis 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=falsesemantics: 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 newUnlistedEntity) 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
There was a problem hiding this comment.
🔵 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=truefrom the former default persistence unit was not migrated here. Hibernate's default isfalse, so this changes update batching behavior and makesDefaultPuPropertiesTest.hibernatePropertiesSurvival()fail; add the corresponding unsupported property alongsideorder_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.xmland have no<class>entries. This early return silently replaces that mapping withno-file; always probe the implicit mapping and let the existinganyMappingFoundfallback 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
|
@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. |
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>
There was a problem hiding this comment.
🔵 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-classesand 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 nativepersistence.xmlprocessing.
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 implicitMETA-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 inMETA-INF/orm.xmlstops working; the linked issue explicitly requires no functional impact, so always probe the implicit mapping and useno-fileonly 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
|
I'll provide one more small change. |
Signed-off-by: Michal Vavřík <dev@michalvavrik.net>
There was a problem hiding this comment.
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.
|
@shawkins whenever you are ready, this can be reviewed. |
There was a problem hiding this comment.
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.
|
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. |
There was a problem hiding this comment.
Thank you @michalvavrik LGTM, I don't any other concerns.
@vmuzikar do you want to take a final look?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
But you already asked to create issue that removes support for the custom persistence xml, so maybe there is no discussion needed anyway.
persistence.xmlto Quarkus Hibernate properties #51121