Skip to content

Commit

Permalink
Merge pull request #30328 from yrodiere/i12685-ignore-persistence-xml
Browse files Browse the repository at this point in the history
Add configuration property `quarkus.hibernate-orm.persistence-xml.ignore`
  • Loading branch information
yrodiere authored Jan 12, 2023
2 parents cfb9017 + c99696b commit f1be907
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
24 changes: 20 additions & 4 deletions docs/src/main/asciidoc/hibernate-orm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<persistence-xml,`persistence.xml`>> 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]
====
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public class HibernateOrmConfig {
@ConfigItem(name = ConfigItem.PARENT)
public Map<String, HibernateOrmConfigPersistenceUnit> persistenceUnits;

/**
* Configuration for the {@code persistence.xml} handling.
*/
@ConfigItem
public HibernateOrmConfigPersistenceXml persistenceXml;

/**
* Logging configuration.
*/
Expand Down Expand Up @@ -86,6 +92,18 @@ public Map<String, HibernateOrmConfigPersistenceUnit> 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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,10 @@ public void enrollBeanValidationTypeSafeActivatorForReflection(Capabilities capa
}

@BuildStep
List<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles(LaunchModeBuildItem launchMode) {
List<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles(HibernateOrmConfig config,
LaunchModeBuildItem launchMode) {
List<HotDeploymentWatchedFileBuildItem> watchedFiles = new ArrayList<>();
if (shouldIgnorePersistenceXmlResources()) {
if (!shouldIgnorePersistenceXmlResources(config)) {
watchedFiles.add(new HotDeploymentWatchedFileBuildItem("META-INF/persistence.xml"));
}
watchedFiles.add(new HotDeploymentWatchedFileBuildItem(INTEGRATOR_SERVICE_FILE));
Expand All @@ -335,9 +336,9 @@ List<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles(LaunchModeBuil

//Integration point: allow other extensions to define additional PersistenceXmlDescriptorBuildItem
@BuildStep
public void parsePersistenceXmlDescriptors(
public void parsePersistenceXmlDescriptors(HibernateOrmConfig config,
BuildProducer<PersistenceXmlDescriptorBuildItem> persistenceXmlDescriptorBuildItemBuildProducer) {
if (!shouldIgnorePersistenceXmlResources()) {
if (!shouldIgnorePersistenceXmlResources(config)) {
List<ParsedPersistenceXmlDescriptor> explicitDescriptors = QuarkusPersistenceXmlParser.locatePersistenceUnits();
for (ParsedPersistenceXmlDescriptor desc : explicitDescriptors) {
persistenceXmlDescriptorBuildItemBuildProducer.produce(new PersistenceXmlDescriptorBuildItem(desc));
Expand Down Expand Up @@ -1429,14 +1430,18 @@ private static Collection<AnnotationInstance> 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.
* <p>
* The main way to ignore {@code persistence.xml} files is to set the configuration property
* {@code quarkus.hibernate-orm.persistence-xml.ignore}.
* <p>
* 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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -36,7 +32,7 @@ public class ExcludePersistenceXmlConfigTest {
Instance<EntityManager> entityManagers;

@Test
public void testPersistenceAndConfigTest() {
public void puIsFromApplicationProperties() {
// We have an entity manager
Assertions.assertNotNull(entityManager);
// We have exactly one entity manager
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit f1be907

Please sign in to comment.