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..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 @@ -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/ExcludePersistenceXmlConfigTest.java b/extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java similarity index 56% rename from extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ExcludePersistenceXmlConfigTest.java rename to extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java index be111e267ab16..3a78729cbcda7 100644 --- 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/xml/persistence/ExcludePersistenceXmlConfigUsingApplicationPropertiesTest.java @@ -1,33 +1,29 @@ -package io.quarkus.hibernate.orm; +package io.quarkus.hibernate.orm.xml.persistence; -import static org.hibernate.cfg.AvailableSettings.PERSISTENCE_UNIT_NAME; +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.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"; +public class ExcludePersistenceXmlConfigUsingApplicationPropertiesTest { @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")); + .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; @@ -36,7 +32,7 @@ public class ExcludePersistenceXmlConfigTest { Instance entityManagers; @Test - public void testPersistenceAndConfigTest() { + public void puIsFromApplicationProperties() { // We have an entity manager Assertions.assertNotNull(entityManager); // We have exactly one entity manager @@ -45,10 +41,22 @@ public void testPersistenceAndConfigTest() { 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)); + 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";