From beba51aa129f02ff79411a7187aa2fe3fdee8d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 12 Jan 2023 09:13:54 +0100 Subject: [PATCH 1/3] Remove duplicate test for SKIP_PARSE_PERSISTENCE_XML We already have io.quarkus.hibernate.orm.xml.persistence.ExcludePersistenceXmlConfigTest, and it's more comprehensive. --- .../orm/ExcludePersistenceXmlConfigTest.java | 54 ------------------- 1 file changed, 54 deletions(-) delete mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ExcludePersistenceXmlConfigTest.java diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ExcludePersistenceXmlConfigTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ExcludePersistenceXmlConfigTest.java deleted file mode 100644 index be111e267ab16..0000000000000 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ExcludePersistenceXmlConfigTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package io.quarkus.hibernate.orm; - -import static org.hibernate.cfg.AvailableSettings.PERSISTENCE_UNIT_NAME; - -import javax.enterprise.inject.Instance; -import javax.inject.Inject; -import javax.persistence.EntityManager; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - -import io.quarkus.arc.Arc; -import io.quarkus.hibernate.orm.enhancer.Address; -import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil; -import io.quarkus.test.QuarkusUnitTest; - -public class ExcludePersistenceXmlConfigTest { - - //The system property used by the Hibernate ORM extension to disable parsing of persistence.xml resources: - private static String SKIP_PARSE_PERSISTENCE_XML = "SKIP_PARSE_PERSISTENCE_XML"; - - @RegisterExtension - static QuarkusUnitTest runner = new QuarkusUnitTest() - .setBeforeAllCustomizer(() -> System.setProperty(SKIP_PARSE_PERSISTENCE_XML, "true")) - .setAfterAllCustomizer(() -> System.getProperties().remove(SKIP_PARSE_PERSISTENCE_XML)) - .withApplicationRoot((jar) -> jar - .addClass(Address.class) - .addAsManifestResource("META-INF/some-persistence.xml", "persistence.xml") - .addAsResource("application.properties")); - - @Inject - EntityManager entityManager; - - @Inject - Instance entityManagers; - - @Test - public void testPersistenceAndConfigTest() { - // We have an entity manager - Assertions.assertNotNull(entityManager); - // We have exactly one entity manager - Assertions.assertEquals(false, entityManagers.isAmbiguous()); - Arc.container().requestContext().activate(); - try { - // it is the default entity manager from application.properties, not templatePU from the persistence.xml - Assertions.assertEquals(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, - entityManager.getEntityManagerFactory().getProperties().get(PERSISTENCE_UNIT_NAME)); - } finally { - Arc.container().requestContext().deactivate(); - } - } - -} From 17d67b9b97d26deadca899ca36c595c69136a09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 12 Jan 2023 09:17:15 +0100 Subject: [PATCH 2/3] Add configuration property quarkus.hibernate-orm.persistence-xml.ignore Fixes #12685 --- docs/src/main/asciidoc/hibernate-orm.adoc | 24 +++++-- .../orm/deployment/HibernateOrmConfig.java | 18 ++++++ .../orm/deployment/HibernateOrmProcessor.java | 23 ++++--- ...lConfigUsingApplicationPropertiesTest.java | 62 +++++++++++++++++++ ...enceXmlConfigUsingSystemPropertyTest.java} | 2 +- 5 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java rename extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/{ExcludePersistenceXmlConfigTest.java => ExcludePersistenceXmlConfigUsingSystemPropertyTest.java} (97%) diff --git a/docs/src/main/asciidoc/hibernate-orm.adoc b/docs/src/main/asciidoc/hibernate-orm.adoc index 093c6c9187441..a5d8b2398b44f 100644 --- a/docs/src/main/asciidoc/hibernate-orm.adoc +++ b/docs/src/main/asciidoc/hibernate-orm.adoc @@ -180,11 +180,19 @@ The configuration properties listed here allow you to override such defaults, an include::{generated-dir}/config/quarkus-hibernate-orm.adoc[opts=optional, leveloffset=+2] [NOTE] --- -Do not mix `persistence.xml` and `quarkus.hibernate-orm.*` properties in `{config-file}`. +==== +Do not mix <> and `quarkus.hibernate-orm.*` properties in `{config-file}`. Quarkus will raise an exception. Make up your mind on which approach you want to use. --- + +If your classpath contains a `persistence.xml` that you want to ignore, +set the following configuration property: + +[source,properties] +---- +quarkus.hibernate-orm.persistence-xml.ignore=true +---- +==== [TIP] ==== @@ -379,8 +387,16 @@ This is useful for: [NOTE] ==== -If you have a `persistence.xml`, then you cannot use the `quarkus.hibernate-orm.*` properties +If you use a `persistence.xml`, then you cannot use the `quarkus.hibernate-orm.*` properties and only persistence units defined in `persistence.xml` will be taken into account. + +If your classpath contains a `persistence.xml` that you want to ignore, +set the following configuration property: + +[source,properties] +---- +quarkus.hibernate-orm.persistence-xml.ignore=true +---- ==== Your `pom.xml` dependencies as well as your Java code would be identical to the precedent example. The only diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java index f32da2be468d5..ee7a11a7d03b1 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmConfig.java @@ -40,6 +40,12 @@ public class HibernateOrmConfig { @ConfigItem(name = ConfigItem.PARENT) public Map persistenceUnits; + /** + * Configuration for the {@code persistence.xml} handling. + */ + @ConfigItem + public HibernateOrmConfigPersistenceXml persistenceXml; + /** * Logging configuration. */ @@ -86,6 +92,18 @@ public Map getAllPersistenceUnitConfi return map; } + @ConfigGroup + public static class HibernateOrmConfigPersistenceXml { + + /** + * If {@code true}, Quarkus will ignore any {@code persistence.xml} file in the classpath + * and rely exclusively on the Quarkus configuration. + */ + @ConfigItem + public boolean ignore; + + } + @ConfigGroup public static class HibernateOrmConfigLog { diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index 0159dc8f6fa87..cf2ab27b7528f 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -321,9 +321,10 @@ public void enrollBeanValidationTypeSafeActivatorForReflection(Capabilities capa } @BuildStep - List hotDeploymentWatchedFiles(LaunchModeBuildItem launchMode) { + List hotDeploymentWatchedFiles(HibernateOrmConfig config, + LaunchModeBuildItem launchMode) { List watchedFiles = new ArrayList<>(); - if (shouldIgnorePersistenceXmlResources()) { + if (shouldIgnorePersistenceXmlResources(config)) { watchedFiles.add(new HotDeploymentWatchedFileBuildItem("META-INF/persistence.xml")); } watchedFiles.add(new HotDeploymentWatchedFileBuildItem(INTEGRATOR_SERVICE_FILE)); @@ -335,9 +336,9 @@ List hotDeploymentWatchedFiles(LaunchModeBuil //Integration point: allow other extensions to define additional PersistenceXmlDescriptorBuildItem @BuildStep - public void parsePersistenceXmlDescriptors( + public void parsePersistenceXmlDescriptors(HibernateOrmConfig config, BuildProducer persistenceXmlDescriptorBuildItemBuildProducer) { - if (!shouldIgnorePersistenceXmlResources()) { + if (!shouldIgnorePersistenceXmlResources(config)) { List explicitDescriptors = QuarkusPersistenceXmlParser.locatePersistenceUnits(); for (ParsedPersistenceXmlDescriptor desc : explicitDescriptors) { persistenceXmlDescriptorBuildItemBuildProducer.produce(new PersistenceXmlDescriptorBuildItem(desc)); @@ -1429,14 +1430,18 @@ private static Collection getPackageLevelPersistenceUnitAnno } /** - * Undocumented feature: we allow setting the System property - * "SKIP_PARSE_PERSISTENCE_XML" to fully ignore any persistence.xml - * resource. + * Checks whether we should ignore {@code persistence.xml} files. + *

+ * The main way to ignore {@code persistence.xml} files is to set the configuration property + * {@code quarkus.hibernate-orm.persistence-xml.ignore}. + *

+ * But there is also an undocumented feature: we allow setting the System property + * "SKIP_PARSE_PERSISTENCE_XML" to ignore any {@code persistence.xml} resource. * * @return true if we're expected to ignore them */ - private boolean shouldIgnorePersistenceXmlResources() { - return Boolean.getBoolean("SKIP_PARSE_PERSISTENCE_XML"); + private boolean shouldIgnorePersistenceXmlResources(HibernateOrmConfig config) { + return config.persistenceXml.ignore || Boolean.getBoolean("SKIP_PARSE_PERSISTENCE_XML"); } /** diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java new file mode 100644 index 0000000000000..3a78729cbcda7 --- /dev/null +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java @@ -0,0 +1,62 @@ +package io.quarkus.hibernate.orm.xml.persistence; + +import static org.assertj.core.api.Assertions.assertThat; + +import javax.enterprise.inject.Instance; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.transaction.Transactional; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil; +import io.quarkus.test.QuarkusUnitTest; + +public class ExcludePersistenceXmlConfigUsingApplicationPropertiesTest { + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClass(MyEntity.class) + .addAsManifestResource("META-INF/some-persistence.xml", "persistence.xml")) + .withConfigurationResource("application.properties") + .overrideConfigKey("quarkus.hibernate-orm.persistence-xml.ignore", "true"); + + @Inject + EntityManager entityManager; + + @Inject + Instance entityManagers; + + @Test + public void puIsFromApplicationProperties() { + // We have an entity manager + Assertions.assertNotNull(entityManager); + // We have exactly one entity manager + Assertions.assertEquals(false, entityManagers.isAmbiguous()); + Arc.container().requestContext().activate(); + try { + // it is the default entity manager from application.properties, not templatePU from the persistence.xml + Assertions.assertEquals(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME, + entityManager.getEntityManagerFactory().getProperties() + .get(org.hibernate.cfg.AvailableSettings.PERSISTENCE_UNIT_NAME)); + } finally { + Arc.container().requestContext().deactivate(); + } + } + + @Test + @Transactional + public void smokeTest() { + MyEntity persistedEntity = new MyEntity("someName"); + entityManager.persist(persistedEntity); + entityManager.flush(); + entityManager.clear(); + MyEntity retrievedEntity = entityManager.find(MyEntity.class, persistedEntity.id); + assertThat(retrievedEntity.name).isEqualTo(persistedEntity.name); + } + +} diff --git a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingSystemPropertyTest.java similarity index 97% rename from extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigTest.java rename to extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingSystemPropertyTest.java index 52154df3c8de2..c227b301ba371 100644 --- a/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigTest.java +++ b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingSystemPropertyTest.java @@ -15,7 +15,7 @@ import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil; import io.quarkus.test.QuarkusUnitTest; -public class ExcludePersistenceXmlConfigTest { +public class ExcludePersistenceXmlConfigUsingSystemPropertyTest { //The system property used by the Hibernate ORM extension to disable parsing of persistence.xml resources: private static final String SKIP_PARSE_PERSISTENCE_XML = "SKIP_PARSE_PERSISTENCE_XML"; From c99696b58240fbec488207ae92691db98f3aefaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Thu, 12 Jan 2023 09:38:02 +0100 Subject: [PATCH 3/3] Fix condition for watching persistence.xml files --- .../quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java index cf2ab27b7528f..b7f4897ffe712 100644 --- a/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java +++ b/extensions/hibernate-orm/deployment/src/main/java/io/quarkus/hibernate/orm/deployment/HibernateOrmProcessor.java @@ -324,7 +324,7 @@ public void enrollBeanValidationTypeSafeActivatorForReflection(Capabilities capa List hotDeploymentWatchedFiles(HibernateOrmConfig config, LaunchModeBuildItem launchMode) { List watchedFiles = new ArrayList<>(); - if (shouldIgnorePersistenceXmlResources(config)) { + if (!shouldIgnorePersistenceXmlResources(config)) { watchedFiles.add(new HotDeploymentWatchedFileBuildItem("META-INF/persistence.xml")); } watchedFiles.add(new HotDeploymentWatchedFileBuildItem(INTEGRATOR_SERVICE_FILE));