diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java index 55e7ed2cae..94e1dea153 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java @@ -471,17 +471,14 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil final ConfigLoader configLoader = new ConfigLoader( ssrBuilder.getBootstrapServiceRegistry() ); // see if the persistence.xml settings named a Hibernate config file.... - final String cfgXmlResourceName1 = (String) mergedSettings.configurationValues.remove( CFG_FILE ); - if ( StringHelper.isNotEmpty( cfgXmlResourceName1 ) ) { - final LoadedConfig loadedCfg = configLoader.loadConfigXmlResource( cfgXmlResourceName1 ); - processConfigXml( loadedCfg, mergedSettings, ssrBuilder ); + String cfgXmlResourceName = (String) mergedSettings.configurationValues.remove( CFG_FILE ); + if ( StringHelper.isEmpty( cfgXmlResourceName ) ) { + // see if integration settings named a Hibernate config file.... + cfgXmlResourceName = (String) integrationSettings.get( CFG_FILE ); } - // see if integration settings named a Hibernate config file.... - final String cfgXmlResourceName2 = (String) integrationSettings.get( CFG_FILE ); - if ( StringHelper.isNotEmpty( cfgXmlResourceName2 ) ) { - integrationSettings.remove( CFG_FILE ); - final LoadedConfig loadedCfg = configLoader.loadConfigXmlResource( cfgXmlResourceName2 ); + if ( StringHelper.isNotEmpty( cfgXmlResourceName ) ) { + final LoadedConfig loadedCfg = configLoader.loadConfigXmlResource( cfgXmlResourceName ); processConfigXml( loadedCfg, mergedSettings, ssrBuilder ); } diff --git a/hibernate-core/src/test/java/org/hibernate/cfg/CfgFilePropertyTest.java b/hibernate-core/src/test/java/org/hibernate/cfg/CfgFilePropertyTest.java new file mode 100644 index 0000000000..a58fd2e10b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/cfg/CfgFilePropertyTest.java @@ -0,0 +1,66 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.cfg; + +import java.io.IOException; +import java.net.URL; +import java.util.Collections; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicReference; +import javax.persistence.Persistence; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.Test; + +import static org.hibernate.internal.util.ConfigHelper.findAsResource; +import static org.junit.Assert.assertNull; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue(jiraKey = "HHH-13227") +public class CfgFilePropertyTest extends BaseUnitTestCase { + + @Test + public void test() throws InterruptedException { + + final AtomicReference exceptionHolder = new AtomicReference<>(); + + Thread thread = new Thread( () -> { + try { + final Properties props = new Properties(); + props.setProperty( AvailableSettings.CFG_FILE, "/org/hibernate/test/boot/cfgXml/hibernate.cfg.xml" ); + + Persistence.createEntityManagerFactory( "ExcludeUnlistedClassesTest1", props ); + } + catch (Exception e) { + exceptionHolder.set( e ); + } + } ); + thread.setContextClassLoader( new ClassLoader() { + + @Override + protected Enumeration findResources(String name) throws IOException { + return name.equals( "META-INF/persistence.xml" ) ? + Collections.enumeration( + Collections.singletonList( + findAsResource( "org/hibernate/jpa/test/persistenceunit/META-INF/persistence.xml" ) + ) + ) : + Collections.emptyEnumeration(); + } + } ); + + thread.start(); + thread.join(); + + assertNull( exceptionHolder.get() ); + } +}