HHH-14046 Allow customizing the type of SessionFactory by changing the initial ServiceInitiator list

This commit is contained in:
Sanne Grinovero 2020-05-27 22:13:10 +01:00
parent 2baa5fccdc
commit d7373a86bb
5 changed files with 87 additions and 1 deletions

View File

@ -0,0 +1,32 @@
/*
* 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.boot.internal;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.spi.SessionFactoryBuilderService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
public final class DefaultSessionFactoryBuilderInitiator implements StandardServiceInitiator<SessionFactoryBuilderService> {
public static final DefaultSessionFactoryBuilderInitiator INSTANCE = new DefaultSessionFactoryBuilderInitiator();
private DefaultSessionFactoryBuilderInitiator() {
}
@Override
public SessionFactoryBuilderService initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
return DefaultSessionFactoryBuilderService.INSTANCE;
}
@Override
public Class<SessionFactoryBuilderService> getServiceInitiated() {
return SessionFactoryBuilderService.class;
}
}

View File

@ -0,0 +1,29 @@
/*
* 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.boot.internal;
import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.SessionFactoryBuilderImplementor;
import org.hibernate.boot.spi.SessionFactoryBuilderService;
/**
* This is the default implementation of SessionFactoryBuilderService, which just
* returns the default SessionFactoryBuilderImpl.
*/
public final class DefaultSessionFactoryBuilderService implements SessionFactoryBuilderService {
protected static final DefaultSessionFactoryBuilderService INSTANCE = new DefaultSessionFactoryBuilderService();
private DefaultSessionFactoryBuilderService() {
}
@Override
public SessionFactoryBuilderImplementor createSessionFactoryBuilder(final MetadataImpl metadata, final BootstrapContext bootstrapContext) {
return new SessionFactoryBuilderImpl( metadata, bootstrapContext );
}
}

View File

@ -30,6 +30,8 @@ import org.hibernate.boot.spi.BootstrapContext;
import org.hibernate.boot.spi.MetadataBuildingOptions; import org.hibernate.boot.spi.MetadataBuildingOptions;
import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.boot.spi.SessionFactoryBuilderFactory; import org.hibernate.boot.spi.SessionFactoryBuilderFactory;
import org.hibernate.boot.spi.SessionFactoryBuilderImplementor;
import org.hibernate.boot.spi.SessionFactoryBuilderService;
import org.hibernate.cfg.annotations.NamedEntityGraphDefinition; import org.hibernate.cfg.annotations.NamedEntityGraphDefinition;
import org.hibernate.cfg.annotations.NamedProcedureCallDefinition; import org.hibernate.cfg.annotations.NamedProcedureCallDefinition;
import org.hibernate.dialect.function.SQLFunction; import org.hibernate.dialect.function.SQLFunction;
@ -154,7 +156,8 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
@Override @Override
public SessionFactoryBuilder getSessionFactoryBuilder() { public SessionFactoryBuilder getSessionFactoryBuilder() {
final SessionFactoryBuilderImpl defaultBuilder = new SessionFactoryBuilderImpl( this, bootstrapContext ); final SessionFactoryBuilderService factoryBuilderService = metadataBuildingOptions.getServiceRegistry().getService( SessionFactoryBuilderService.class );
final SessionFactoryBuilderImplementor defaultBuilder = factoryBuilderService.createSessionFactoryBuilder( this, bootstrapContext );
final ClassLoaderService cls = metadataBuildingOptions.getServiceRegistry().getService( ClassLoaderService.class ); final ClassLoaderService cls = metadataBuildingOptions.getServiceRegistry().getService( ClassLoaderService.class );
final java.util.Collection<SessionFactoryBuilderFactory> discoveredBuilderFactories = cls.loadJavaServices( SessionFactoryBuilderFactory.class ); final java.util.Collection<SessionFactoryBuilderFactory> discoveredBuilderFactories = cls.loadJavaServices( SessionFactoryBuilderFactory.class );

View File

@ -0,0 +1,19 @@
/*
* 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.boot.spi;
import org.hibernate.boot.internal.MetadataImpl;
import org.hibernate.service.Service;
/**
* Allows bootstrapping Hibernate ORM using a custom {@link SessionFactoryBuilderImplementor}
*/
public interface SessionFactoryBuilderService extends Service {
SessionFactoryBuilderImplementor createSessionFactoryBuilder(MetadataImpl metadata, BootstrapContext bootstrapContext);
}

View File

@ -11,6 +11,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import org.hibernate.boot.cfgxml.internal.CfgXmlAccessServiceInitiator; import org.hibernate.boot.cfgxml.internal.CfgXmlAccessServiceInitiator;
import org.hibernate.boot.internal.DefaultSessionFactoryBuilderInitiator;
import org.hibernate.boot.registry.StandardServiceInitiator; import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.bytecode.internal.BytecodeProviderInitiator; import org.hibernate.bytecode.internal.BytecodeProviderInitiator;
import org.hibernate.bytecode.internal.ProxyFactoryFactoryInitiator; import org.hibernate.bytecode.internal.ProxyFactoryFactoryInitiator;
@ -54,6 +55,8 @@ public final class StandardServiceInitiators {
private static List<StandardServiceInitiator> buildStandardServiceInitiatorList() { private static List<StandardServiceInitiator> buildStandardServiceInitiatorList() {
final ArrayList<StandardServiceInitiator> serviceInitiators = new ArrayList<StandardServiceInitiator>(); final ArrayList<StandardServiceInitiator> serviceInitiators = new ArrayList<StandardServiceInitiator>();
serviceInitiators.add( DefaultSessionFactoryBuilderInitiator.INSTANCE );
serviceInitiators.add( BytecodeProviderInitiator.INSTANCE ); serviceInitiators.add( BytecodeProviderInitiator.INSTANCE );
serviceInitiators.add( ProxyFactoryFactoryInitiator.INSTANCE ); serviceInitiators.add( ProxyFactoryFactoryInitiator.INSTANCE );