HHH-18038 - Fall back to persistence-unit name as SessionFactory name

This commit is contained in:
Steve Ebersole 2024-04-30 10:31:42 -05:00
parent a13d6a385f
commit fea7febff1
3 changed files with 125 additions and 5 deletions

View File

@ -52,6 +52,7 @@ import org.hibernate.cache.cfg.spi.DomainDataRegionConfig;
import org.hibernate.cache.spi.CacheImplementor;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.PersistenceSettings;
import org.hibernate.context.internal.JTASessionContext;
import org.hibernate.context.internal.ManagedSessionContext;
import org.hibernate.context.internal.ThreadLocalSessionContext;
@ -59,6 +60,7 @@ import org.hibernate.context.spi.CurrentSessionContext;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.profile.FetchProfile;
@ -75,6 +77,7 @@ import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.internal.ExceptionMapperLegacyJpaImpl;
import org.hibernate.jpa.internal.PersistenceUnitUtilImpl;
import org.hibernate.mapping.Collection;
@ -141,6 +144,7 @@ import static org.hibernate.cfg.AvailableSettings.CREATE_EMPTY_COMPOSITES_ENABLE
import static org.hibernate.cfg.AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS;
import static org.hibernate.cfg.AvailableSettings.JAKARTA_VALIDATION_FACTORY;
import static org.hibernate.cfg.AvailableSettings.JPA_VALIDATION_FACTORY;
import static org.hibernate.engine.config.spi.StandardConverters.STRING;
import static org.hibernate.internal.FetchProfileHelper.getFetchProfiles;
import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean;
@ -548,13 +552,27 @@ public class SessionFactoryImpl extends QueryParameterBindingTypeResolverImpl im
private static String getSessionFactoryName(SessionFactoryOptions options, SessionFactoryServiceRegistry serviceRegistry) {
final String sessionFactoryName = options.getSessionFactoryName();
if ( sessionFactoryName == null ) {
if ( sessionFactoryName != null ) {
return sessionFactoryName;
}
final CfgXmlAccessService cfgXmlAccessService = serviceRegistry.requireService( CfgXmlAccessService.class );
if ( cfgXmlAccessService.getAggregatedConfig() != null ) {
return cfgXmlAccessService.getAggregatedConfig().getSessionFactoryName();
final String nameFromAggregation = cfgXmlAccessService.getAggregatedConfig().getSessionFactoryName();
if ( nameFromAggregation != null ) {
return nameFromAggregation;
}
}
return sessionFactoryName;
final ConfigurationService configurationService = serviceRegistry.getService( ConfigurationService.class );
assert configurationService != null;
final String puName = configurationService.getSetting( PersistenceSettings.PERSISTENCE_UNIT_NAME, STRING );
if ( puName != null ) {
return puName;
}
return null;
}
private SessionBuilderImpl createDefaultSessionOpenOptionsIfPossible() {

View File

@ -0,0 +1,77 @@
/*
* 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 http://www.gnu.org/licenses/lgpl-2.1.html.
*/
package org.hibernate.orm.test.jpa.compliance;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.boot.archive.scan.internal.DisabledScanner;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.JdbcSettings;
import org.hibernate.cfg.MappingSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.testing.orm.junit.ServiceRegistry;
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
import org.junit.jupiter.api.Test;
import jakarta.persistence.EntityManagerFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Steve Ebersole
*/
public class PersistenceUnitNameTests {
@Test
@ServiceRegistry
void testFirstUnit(ServiceRegistryScope scope) {
try (EntityManagerFactory emf = loadFactory( "first", scope )) {
assertThat( emf.getName() ).isEqualTo( "first" );
assertThat( emf.getName() ).isEqualTo( "first" );
assertThat( emf.getProperties() ).containsEntry( "name", "first" );
}
}
@Test
@ServiceRegistry
void testSecondUnit(ServiceRegistryScope scope) {
try (EntityManagerFactory emf = loadFactory( "second", scope )) {
assertThat( emf.getName() ).isEqualTo( "second" );
assertThat( emf.getProperties() ).containsEntry( "name", "second" );
}
}
private static EntityManagerFactory loadFactory(String name, ServiceRegistryScope scope) {
final URL puFile = PersistenceUnitNameTests.class.getClassLoader().getResource( "xml/jakarta/simple/2units.xml" );
final ParsedPersistenceXmlDescriptor descriptor = PersistenceXmlParser.locateNamedPersistenceUnit( puFile, name );
final EntityManagerFactoryBuilder emfBuilder = Bootstrap.getEntityManagerFactoryBuilder(
descriptor,
buildSettings( scope )
);
return emfBuilder.build();
}
@SuppressWarnings("deprecation")
private static Map<?,?> buildSettings(ServiceRegistryScope scope) {
final ConfigurationService service = scope.getRegistry().getService( ConfigurationService.class );
assert service != null;
final Map<String, Object> allSettings = service.getSettings();
final HashMap<Object, Object> settings = new HashMap<>();
settings.put( JdbcSettings.DRIVER, allSettings.get( JdbcSettings.DRIVER ) );
settings.put( JdbcSettings.USER, allSettings.get( JdbcSettings.USER ) );
settings.put( JdbcSettings.PASS, allSettings.get( JdbcSettings.PASS ) );
settings.put( JdbcSettings.URL, allSettings.get( JdbcSettings.URL ) );
settings.put( AvailableSettings.SCANNER, DisabledScanner.class );
return settings;
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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 http://www.gnu.org/licenses/lgpl-2.1.html.
-->
<!-- example of a default persistence.xml -->
<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.0">
<persistence-unit name="first">
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="name" value="first" />
</properties>
</persistence-unit>
<persistence-unit name="second">
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
<property name="name" value="second" />
</properties>
</persistence-unit>
</persistence>