HHH-7580 - Complete 2-phase SessionFactory building design

Conflicts:
	hibernate-core/src/main/java/org/hibernate/SessionFactory.java
	hibernate-core/src/main/java/org/hibernate/boot/registry/StandardServiceRegistryBuilder.java
	hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java
	hibernate-core/src/main/java/org/hibernate/metamodel/MetadataSources.java
	hibernate-core/src/main/java/org/hibernate/metamodel/SessionFactoryBuilder.java
	hibernate-core/src/main/java/org/hibernate/metamodel/internal/SessionFactoryBuilderImpl.java
	hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JaxbHelper.java
	hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java
	hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java
	hibernate-core/src/main/java/org/hibernate/service/spi/ServiceContributor.java
	hibernate-core/src/test/java/org/hibernate/cfg/beanvalidation/ApplySchemaConstraintTest.java
	hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicCollectionBindingTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/internal/source/AssertSourcesTest.java
	hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/BaseAnnotationBindingTestCase.java
	hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParserTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/source/internal/MetadataImplTest.java
	hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/AbstractBasicBindingTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/AbstractUnsavedValueTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/JoinedSubclassBindingTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/basiccollections/AbstractBasicCollectionBindingTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/spi/binding/onetomany/AbstractUnidirectionalOneToManyBindingTests.java
	hibernate-core/src/test/java/org/hibernate/metamodel/spi/relational/AbstractGeneratedIdColumnTests.java
	hibernate-core/src/test/java/org/hibernate/test/jdbc/internal/BatchingTest.java
	hibernate-core/src/test/java/org/hibernate/test/service/schema/internal/ExistingDatabaseMetaDataImplTest.java
	hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderImpl.java
	hibernate-entitymanager/src/main/java/org/hibernate/jpa/boot/internal/EntityManagerFactoryBuilderUsingMetamodelImpl.java
	hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/NodeEnvironment.java
	hibernate-testing/src/main/java/org/hibernate/testing/junit4/BaseCoreFunctionalTestCase.java
This commit is contained in:
Steve Ebersole 2013-01-29 13:52:35 -05:00 committed by Brett Meyer
parent c05238bad3
commit 7976e2396a
54 changed files with 711 additions and 351 deletions

View File

@ -27,8 +27,10 @@ import java.io.Serializable;
import java.sql.Connection;
import java.util.Map;
import java.util.Set;
import javax.naming.Referenceable;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metadata.CollectionMetadata;
@ -54,8 +56,9 @@ import org.hibernate.stat.Statistics;
public interface SessionFactory extends Referenceable, Serializable {
public interface SessionFactoryOptions {
Interceptor getInterceptor();
EntityNotFoundDelegate getEntityNotFoundDelegate();
public StandardServiceRegistry getServiceRegistry();
public Interceptor getInterceptor();
public EntityNotFoundDelegate getEntityNotFoundDelegate();
}
public SessionFactoryOptions getSessionFactoryOptions();

View File

@ -23,7 +23,9 @@
*/
package org.hibernate.boot.registry;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
import org.hibernate.boot.registry.selector.Availability;
@ -43,10 +45,7 @@ import org.hibernate.boot.registry.selector.internal.StrategySelectorBuilder;
*/
public class BootstrapServiceRegistryBuilder {
private final LinkedHashSet<Integrator> providedIntegrators = new LinkedHashSet<Integrator>();
private ClassLoader applicationClassLoader;
private ClassLoader resourcesClassLoader;
private ClassLoader hibernateClassLoader;
private ClassLoader environmentClassLoader;
private List<ClassLoader> providedClassLoaders;
private StrategySelectorBuilder strategySelectorBuilder = new StrategySelectorBuilder();
@ -61,16 +60,33 @@ public class BootstrapServiceRegistryBuilder {
return this;
}
/**
* Adds a provided {@link ClassLoader} for use in class-loading and resource-lookup
*
* @param classLoader The class loader to use
*
* @return {@code this}, for method chaining
*/
public BootstrapServiceRegistryBuilder with(ClassLoader classLoader) {
if ( providedClassLoaders == null ) {
providedClassLoaders = new ArrayList<ClassLoader>();
}
providedClassLoaders.add( classLoader );
return this;
}
/**
* Applies the specified {@link ClassLoader} as the application class loader for the bootstrap registry
*
* @param classLoader The class loader to use
* @return {@code this}, for method chaining
*
* @deprecated Use {@link #with(ClassLoader)} instead
*/
@Deprecated
@SuppressWarnings( {"UnusedDeclaration"})
public BootstrapServiceRegistryBuilder withApplicationClassLoader(ClassLoader classLoader) {
this.applicationClassLoader = classLoader;
return this;
return with( classLoader );
}
/**
@ -78,11 +94,13 @@ public class BootstrapServiceRegistryBuilder {
*
* @param classLoader The class loader to use
* @return {@code this}, for method chaining
*
* @deprecated Use {@link #with(ClassLoader)} instead
*/
@Deprecated
@SuppressWarnings( {"UnusedDeclaration"})
public BootstrapServiceRegistryBuilder withResourceClassLoader(ClassLoader classLoader) {
this.resourcesClassLoader = classLoader;
return this;
return with( classLoader );
}
/**
@ -90,11 +108,13 @@ public class BootstrapServiceRegistryBuilder {
*
* @param classLoader The class loader to use
* @return {@code this}, for method chaining
*
* @deprecated Use {@link #with(ClassLoader)} instead
*/
@Deprecated
@SuppressWarnings( {"UnusedDeclaration"})
public BootstrapServiceRegistryBuilder withHibernateClassLoader(ClassLoader classLoader) {
this.hibernateClassLoader = classLoader;
return this;
return with( classLoader );
}
/**
@ -102,11 +122,13 @@ public class BootstrapServiceRegistryBuilder {
*
* @param classLoader The class loader to use
* @return {@code this}, for method chaining
*
* @deprecated Use {@link #with(ClassLoader)} instead
*/
@Deprecated
@SuppressWarnings( {"UnusedDeclaration"})
public BootstrapServiceRegistryBuilder withEnvironmentClassLoader(ClassLoader classLoader) {
this.environmentClassLoader = classLoader;
return this;
return with( classLoader );
}
/**
@ -149,12 +171,7 @@ public class BootstrapServiceRegistryBuilder {
* @return The built bootstrap registry
*/
public BootstrapServiceRegistry build() {
final ClassLoaderServiceImpl classLoaderService = new ClassLoaderServiceImpl(
applicationClassLoader,
resourcesClassLoader,
hibernateClassLoader,
environmentClassLoader
);
final ClassLoaderServiceImpl classLoaderService = new ClassLoaderServiceImpl( providedClassLoaders );
final IntegratorServiceImpl integratorService = new IntegratorServiceImpl(
providedIntegrators,

View File

@ -0,0 +1,34 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.boot.registry;
import org.hibernate.service.ServiceRegistry;
/**
* Specialization of the {@link org.hibernate.service.ServiceRegistry} contract mainly for type safety.
*
* @author Steve Ebersole
*/
public interface StandardServiceRegistry extends ServiceRegistry {
}

View File

@ -25,23 +25,24 @@ package org.hibernate.boot.registry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Environment;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.integrator.spi.IntegratorService;
import org.hibernate.integrator.spi.ServiceContributingIntegrator;
import org.hibernate.internal.jaxb.cfg.JaxbHibernateConfiguration;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.boot.registry.internal.BootstrapServiceRegistryImpl;
import org.hibernate.service.ConfigLoader;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.StandardServiceInitiators;
import org.hibernate.service.internal.ProvidedService;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.service.spi.ServiceContributor;
/**
* Builder for standard {@link org.hibernate.service.ServiceRegistry} instances.
@ -65,7 +66,7 @@ public class StandardServiceRegistryBuilder {
* Create a default builder
*/
public StandardServiceRegistryBuilder() {
this( new BootstrapServiceRegistryImpl() );
this( new BootstrapServiceRegistryBuilder().build() );
}
/**
@ -197,26 +198,35 @@ public class StandardServiceRegistryBuilder {
return this;
}
/**
* Build the service registry accounting for all settings and service initiators and services.
*
* @return The built service registry
*/
public ServiceRegistry buildServiceRegistry() {
public StandardServiceRegistry build() {
Map<?,?> settingsCopy = new HashMap();
settingsCopy.putAll( settings );
Environment.verifyProperties( settingsCopy );
ConfigurationHelper.resolvePlaceHolders( settingsCopy );
for ( Integrator integrator : bootstrapServiceRegistry.getService( IntegratorService.class ).getIntegrators() ) {
if ( ServiceContributingIntegrator.class.isInstance( integrator ) ) {
ServiceContributingIntegrator.class.cast( integrator ).prepareServices( this );
}
}
applyServiceContributingIntegrators();
applyServiceContributors();
return new StandardServiceRegistryImpl( bootstrapServiceRegistry, initiators, providedServices, settingsCopy );
}
@SuppressWarnings("deprecation")
private void applyServiceContributingIntegrators() {
for ( Integrator integrator : bootstrapServiceRegistry.getService( IntegratorService.class ).getIntegrators() ) {
if ( org.hibernate.integrator.spi.ServiceContributingIntegrator.class.isInstance( integrator ) ) {
org.hibernate.integrator.spi.ServiceContributingIntegrator.class.cast( integrator ).prepareServices( this );
}
}
}
private void applyServiceContributors() {
LinkedHashSet<ServiceContributor> serviceContributors =
bootstrapServiceRegistry.getService( ClassLoaderService.class ).loadJavaServices( ServiceContributor.class );
for ( ServiceContributor serviceContributor : serviceContributors ) {
serviceContributor.contribute( this );
}
}
/**
* Temporarily exposed since Configuration is still around and much code still uses Configuration. This allows
* code to configure the builder and access that to configure Configuration object (used from HEM atm).

View File

@ -27,6 +27,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
@ -49,75 +51,71 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
public class ClassLoaderServiceImpl implements ClassLoaderService {
private static final Logger log = Logger.getLogger( ClassLoaderServiceImpl.class );
private final ClassLoader classClassLoader;
private final ClassLoader resourcesClassLoader;
private final ClassLoader serviceLoaderClassLoader;
private final ClassLoader aggregatedClassLoader;
public ClassLoaderServiceImpl() {
this( ClassLoaderServiceImpl.class.getClassLoader() );
}
public ClassLoaderServiceImpl(ClassLoader classLoader) {
this( classLoader, classLoader, classLoader, classLoader );
this( Collections.singletonList( classLoader ) );
}
public ClassLoaderServiceImpl(
ClassLoader applicationClassLoader,
ClassLoader resourcesClassLoader,
ClassLoader hibernateClassLoader,
ClassLoader environmentClassLoader) {
// Normalize missing loaders
if ( hibernateClassLoader == null ) {
hibernateClassLoader = ClassLoaderServiceImpl.class.getClassLoader();
}
public ClassLoaderServiceImpl(List<ClassLoader> providedClassLoaders) {
final LinkedHashSet<ClassLoader> orderedClassLoaderSet = new LinkedHashSet<ClassLoader>();
if ( environmentClassLoader == null || applicationClassLoader == null ) {
ClassLoader sysClassLoader = locateSystemClassLoader();
ClassLoader tccl = locateTCCL();
if ( environmentClassLoader == null ) {
environmentClassLoader = sysClassLoader != null ? sysClassLoader : hibernateClassLoader;
}
if ( applicationClassLoader == null ) {
applicationClassLoader = tccl != null ? tccl : hibernateClassLoader;
}
}
if ( resourcesClassLoader == null ) {
resourcesClassLoader = applicationClassLoader;
}
final LinkedHashSet<ClassLoader> classLoadingClassLoaders = new LinkedHashSet<ClassLoader>();
classLoadingClassLoaders.add( applicationClassLoader );
classLoadingClassLoaders.add( hibernateClassLoader );
classLoadingClassLoaders.add( environmentClassLoader );
this.classClassLoader = new ClassLoader(null) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for ( ClassLoader loader : classLoadingClassLoaders ) {
try {
return loader.loadClass( name );
}
catch (Exception ignore) {
}
// first add all provided class loaders, if any
if ( providedClassLoaders != null ) {
for ( ClassLoader classLoader : providedClassLoaders ) {
if ( classLoader != null ) {
orderedClassLoaderSet.add( classLoader );
}
throw new ClassNotFoundException( "Could not load requested class : " + name );
}
};
}
this.resourcesClassLoader = resourcesClassLoader;
// normalize adding known class-loaders...
// first the Hibernate class loader
orderedClassLoaderSet.add( ClassLoaderServiceImpl.class.getClassLoader() );
// then the TCCL, if one...
final ClassLoader tccl = locateTCCL();
if ( tccl != null ) {
orderedClassLoaderSet.add( tccl );
}
// finally the system classloader
final ClassLoader sysClassLoader = locateSystemClassLoader();
if ( sysClassLoader != null ) {
orderedClassLoaderSet.add( sysClassLoader );
}
this.serviceLoaderClassLoader = buildServiceLoaderClassLoader();
// now build the aggregated class loader...
this.aggregatedClassLoader = new AggregatedClassLoader( orderedClassLoaderSet );
}
@SuppressWarnings( {"UnusedDeclaration"})
@SuppressWarnings({"UnusedDeclaration", "unchecked", "deprecation"})
@Deprecated
public static ClassLoaderServiceImpl fromConfigSettings(Map configVales) {
return new ClassLoaderServiceImpl(
(ClassLoader) configVales.get( AvailableSettings.APP_CLASSLOADER ),
(ClassLoader) configVales.get( AvailableSettings.RESOURCES_CLASSLOADER ),
(ClassLoader) configVales.get( AvailableSettings.HIBERNATE_CLASSLOADER ),
(ClassLoader) configVales.get( AvailableSettings.ENVIRONMENT_CLASSLOADER )
);
final List<ClassLoader> providedClassLoaders = new ArrayList<ClassLoader>();
final Collection<ClassLoader> classLoaders = (Collection<ClassLoader>) configVales.get( AvailableSettings.CLASSLOADERS );
if ( classLoaders != null ) {
for ( ClassLoader classLoader : classLoaders ) {
providedClassLoaders.add( classLoader );
}
}
addIfSet( providedClassLoaders, AvailableSettings.APP_CLASSLOADER, configVales );
addIfSet( providedClassLoaders, AvailableSettings.RESOURCES_CLASSLOADER, configVales );
addIfSet( providedClassLoaders, AvailableSettings.HIBERNATE_CLASSLOADER, configVales );
addIfSet( providedClassLoaders, AvailableSettings.ENVIRONMENT_CLASSLOADER, configVales );
return new ClassLoaderServiceImpl( providedClassLoaders );
}
private static void addIfSet(List<ClassLoader> providedClassLoaders, String name, Map configVales) {
final ClassLoader providedClassLoader = (ClassLoader) configVales.get( name );
if ( providedClassLoader != null ) {
providedClassLoaders.add( providedClassLoader );
}
}
private static ClassLoader locateSystemClassLoader() {
@ -138,73 +136,69 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
}
}
private ClassLoader buildServiceLoaderClassLoader() {
return new ClassLoader(null) {
final ClassLoader[] classLoaderArray = new ClassLoader[] {
// first look on the hibernate class loader
getClass().getClassLoader(),
// next look on the resource class loader
resourcesClassLoader,
// finally look on the combined class class loader
classClassLoader
private static class AggregatedClassLoader extends ClassLoader {
private final ClassLoader[] individualClassLoaders;
private AggregatedClassLoader(final LinkedHashSet<ClassLoader> orderedClassLoaderSet) {
super( null );
individualClassLoaders = orderedClassLoaderSet.toArray( new ClassLoader[ orderedClassLoaderSet.size() ] );
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
final HashSet<URL> resourceUrls = new HashSet<URL>();
for ( ClassLoader classLoader : individualClassLoaders ) {
final Enumeration<URL> urls = classLoader.getResources( name );
while ( urls.hasMoreElements() ) {
resourceUrls.add( urls.nextElement() );
}
}
return new Enumeration<URL>() {
final Iterator<URL> resourceUrlIterator = resourceUrls.iterator();
@Override
public boolean hasMoreElements() {
return resourceUrlIterator.hasNext();
}
@Override
public URL nextElement() {
return resourceUrlIterator.next();
}
};
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
final HashSet<URL> resourceUrls = new HashSet<URL>();
for ( ClassLoader classLoader : classLoaderArray ) {
final Enumeration<URL> urls = classLoader.getResources( name );
while ( urls.hasMoreElements() ) {
resourceUrls.add( urls.nextElement() );
}
@Override
protected URL findResource(String name) {
for ( ClassLoader classLoader : individualClassLoaders ) {
final URL resource = classLoader.getResource( name );
if ( resource != null ) {
return resource;
}
}
return super.findResource( name );
}
return new Enumeration<URL>() {
final Iterator<URL> resourceUrlIterator = resourceUrls.iterator();
@Override
public boolean hasMoreElements() {
return resourceUrlIterator.hasNext();
}
@Override
public URL nextElement() {
return resourceUrlIterator.next();
}
};
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for ( ClassLoader classLoader : individualClassLoaders ) {
try {
return classLoader.loadClass( name );
}
catch (Exception ignore) {
}
}
@Override
protected URL findResource(String name) {
for ( ClassLoader classLoader : classLoaderArray ) {
final URL resource = classLoader.getResource( name );
if ( resource != null ) {
return resource;
}
}
return super.findResource( name );
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
for ( ClassLoader classLoader : classLoaderArray ) {
try {
return classLoader.loadClass( name );
}
catch (Exception ignore) {
}
}
throw new ClassNotFoundException( "Could not load requested class : " + name );
}
};
throw new ClassNotFoundException( "Could not load requested class : " + name );
}
}
@Override
@SuppressWarnings( {"unchecked"})
public <T> Class<T> classForName(String className) {
try {
return (Class<T>) Class.forName( className, true, classClassLoader );
return (Class<T>) Class.forName( className, true, aggregatedClassLoader );
}
catch (Exception e) {
throw new ClassLoadingException( "Unable to load class [" + className + "]", e );
@ -221,7 +215,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
}
try {
return resourcesClassLoader.getResource( name );
return aggregatedClassLoader.getResource( name );
}
catch ( Exception ignore ) {
}
@ -241,7 +235,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
try {
log.tracef( "trying via [ClassLoader.getResourceAsStream(\"%s\")]", name );
InputStream stream = resourcesClassLoader.getResourceAsStream( name );
InputStream stream = aggregatedClassLoader.getResourceAsStream( name );
if ( stream != null ) {
return stream;
}
@ -261,7 +255,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
try {
log.tracef( "trying via [ClassLoader.getResourceAsStream(\"%s\")]", stripped );
InputStream stream = resourcesClassLoader.getResourceAsStream( stripped );
InputStream stream = aggregatedClassLoader.getResourceAsStream( stripped );
if ( stream != null ) {
return stream;
}
@ -277,7 +271,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
public List<URL> locateResources(String name) {
ArrayList<URL> urls = new ArrayList<URL>();
try {
Enumeration<URL> urlEnumeration = resourcesClassLoader.getResources( name );
Enumeration<URL> urlEnumeration = aggregatedClassLoader.getResources( name );
if ( urlEnumeration != null && urlEnumeration.hasMoreElements() ) {
while ( urlEnumeration.hasMoreElements() ) {
urls.add( urlEnumeration.nextElement() );
@ -292,7 +286,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
@Override
public <S> LinkedHashSet<S> loadJavaServices(Class<S> serviceContract) {
final ServiceLoader<S> loader = ServiceLoader.load( serviceContract, serviceLoaderClassLoader );
final ServiceLoader<S> loader = ServiceLoader.load( serviceContract, aggregatedClassLoader );
final LinkedHashSet<S> services = new LinkedHashSet<S>();
for ( S service : loader ) {
services.add( service );
@ -315,7 +309,7 @@ public class ClassLoaderServiceImpl implements ClassLoaderService {
boolean set = false;
try {
Thread.currentThread().setContextClassLoader( serviceLoaderClassLoader);
Thread.currentThread().setContextClassLoader( aggregatedClassLoader );
set = true;
}
catch (Exception ignore) {

View File

@ -28,8 +28,8 @@ import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.internal.AbstractServiceRegistryImpl;
import org.hibernate.service.internal.ProvidedService;
import org.hibernate.service.spi.Configurable;
@ -41,7 +41,7 @@ import org.hibernate.service.spi.ServiceInitiator;
*
* @author Steve Ebersole
*/
public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl implements ServiceRegistry {
public class StandardServiceRegistryImpl extends AbstractServiceRegistryImpl implements StandardServiceRegistry {
private final Map configurationValues;
@SuppressWarnings( {"unchecked"})

View File

@ -477,30 +477,47 @@ public interface AvailableSettings {
*/
public static final String NON_CONTEXTUAL_LOB_CREATION = "hibernate.jdbc.lob.non_contextual_creation";
/**
* Used to define a {@link java.util.Collection} of the {@link ClassLoader} instances Hibernate should use for
* class-loading and resource-lookups.
*
* @since 5.0
*/
public static final String CLASSLOADERS = "hibernate.classLoaders";
/**
* Names the {@link ClassLoader} used to load user application classes.
* @since 4.0
*
* @deprecated Use {@link #CLASSLOADERS} instead
*/
@Deprecated
public static final String APP_CLASSLOADER = "hibernate.classLoader.application";
/**
* Names the {@link ClassLoader} Hibernate should use to perform resource loading.
* @since 4.0
* @deprecated Use {@link #CLASSLOADERS} instead
*/
@Deprecated
public static final String RESOURCES_CLASSLOADER = "hibernate.classLoader.resources";
/**
* Names the {@link ClassLoader} responsible for loading Hibernate classes. By default this is
* the {@link ClassLoader} that loaded this class.
* @since 4.0
* @deprecated Use {@link #CLASSLOADERS} instead
*/
@Deprecated
public static final String HIBERNATE_CLASSLOADER = "hibernate.classLoader.hibernate";
/**
* Names the {@link ClassLoader} used when Hibernate is unable to locates classes on the
* {@link #APP_CLASSLOADER} or {@link #HIBERNATE_CLASSLOADER}.
* @since 4.0
* @deprecated Use {@link #CLASSLOADERS} instead
*/
@Deprecated
public static final String ENVIRONMENT_CLASSLOADER = "hibernate.classLoader.environment";

View File

@ -1768,7 +1768,7 @@ public class Configuration implements Serializable {
ConfigurationHelper.resolvePlaceHolders( properties );
final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings( properties )
.buildServiceRegistry();
.build();
setSessionFactoryObserver(
new SessionFactoryObserver() {
@Override

View File

@ -66,7 +66,7 @@ import org.hibernate.internal.util.config.ConfigurationHelper;
* Properties may be either be <tt>System</tt> properties, properties
* defined in a resource named <tt>/hibernate.properties</tt> or an instance of
* <tt>java.util.Properties</tt> passed to
* <tt>Configuration.buildSessionFactory()</tt><br>
* <tt>Configuration.build()</tt><br>
* <br>
* <table>
* <tr><td><b>property</b></td><td><b>meaning</b></td></tr>

View File

@ -3040,7 +3040,7 @@ public final class HbmBinder {
//TODO: bad implementation, cos it depends upon ordering of mapping doc
// fixing this requires that Collection/PersistentClass gain access
// to the Mappings reference from Configuration (or the filterDefinitions
// map directly) sometime during Configuration.buildSessionFactory
// map directly) sometime during Configuration.build
// (after all the types/filter-defs are known and before building
// persisters).
if ( StringHelper.isEmpty(condition) ) {

View File

@ -23,8 +23,6 @@
*/
package org.hibernate.internal;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
@ -42,7 +40,8 @@ import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.jboss.logging.Logger;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import org.hibernate.AssertionFailure;
import org.hibernate.Cache;
@ -63,6 +62,9 @@ import org.hibernate.SessionFactoryObserver;
import org.hibernate.StatelessSession;
import org.hibernate.StatelessSessionBuilder;
import org.hibernate.TypeHelper;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.cache.internal.CacheDataDescriptionImpl;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.EntityRegion;
@ -90,10 +92,13 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.dialect.function.SQLFunctionRegistry;
import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.spi.JdbcConnectionAccess;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
import org.hibernate.engine.jndi.spi.JndiService;
import org.hibernate.engine.profile.Association;
import org.hibernate.engine.profile.Fetch;
import org.hibernate.engine.profile.FetchProfile;
@ -108,6 +113,7 @@ import org.hibernate.engine.spi.SessionBuilderImplementor;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionOwner;
import org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.engine.transaction.spi.TransactionEnvironment;
import org.hibernate.engine.transaction.spi.TransactionFactory;
import org.hibernate.exception.spi.SQLExceptionConverter;
@ -131,12 +137,6 @@ import org.hibernate.persister.entity.Queryable;
import org.hibernate.persister.spi.PersisterFactory;
import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jndi.spi.JndiService;
import org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.hibernate.service.spi.SessionFactoryServiceRegistryFactory;
@ -150,6 +150,7 @@ import org.hibernate.tuple.entity.EntityTuplizer;
import org.hibernate.type.AssociationType;
import org.hibernate.type.Type;
import org.hibernate.type.TypeResolver;
import org.jboss.logging.Logger;
/**
@ -220,7 +221,7 @@ public final class SessionFactoryImpl
public SessionFactoryImpl(
final Configuration cfg,
Mapping mapping,
ServiceRegistry serviceRegistry,
final ServiceRegistry serviceRegistry,
Settings settings,
SessionFactoryObserver observer) throws HibernateException {
LOG.debug( "Building session factory" );
@ -228,6 +229,11 @@ public final class SessionFactoryImpl
sessionFactoryOptions = new SessionFactoryOptions() {
private EntityNotFoundDelegate entityNotFoundDelegate;
@Override
public StandardServiceRegistry getServiceRegistry() {
return (StandardServiceRegistry) serviceRegistry;
}
@Override
public Interceptor getInterceptor() {
return cfg.getInterceptor();
@ -666,7 +672,7 @@ public final class SessionFactoryImpl
);
this.serviceRegistry =
metadata.getServiceRegistry()
sessionFactoryOptions.getServiceRegistry()
.getService( SessionFactoryServiceRegistryFactory.class )
.buildServiceRegistry( this, metadata );

View File

@ -28,6 +28,7 @@ import java.util.Map;
import javax.persistence.SharedCacheMode;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.ResultSetMappingDefinition;
@ -48,6 +49,8 @@ public interface Metadata {
* Exposes the options used to produce a {@link Metadata} instance.
*/
public static interface Options {
public StandardServiceRegistry getServiceRegistry();
public MetadataSourceProcessingOrder getMetadataSourceProcessingOrder();
public NamingStrategy getNamingStrategy();
public SharedCacheMode getSharedCacheMode();

View File

@ -43,5 +43,5 @@ public interface MetadataBuilder {
public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled);
public Metadata buildMetadata();
public Metadata build();
}

View File

@ -36,10 +36,9 @@ import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.cfg.EJB3DTDEntityResolver;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
@ -51,7 +50,9 @@ import org.hibernate.metamodel.source.MappingNotFoundException;
import org.hibernate.metamodel.source.internal.JaxbHelper;
import org.hibernate.metamodel.source.internal.MetadataBuilderImpl;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
/**
* @author Steve Ebersole
@ -59,13 +60,14 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
public class MetadataSources {
private static final Logger LOG = Logger.getLogger( MetadataSources.class );
private final ServiceRegistry serviceRegistry;
private List<JaxbRoot> jaxbRootList = new ArrayList<JaxbRoot>();
private LinkedHashSet<Class<?>> annotatedClasses = new LinkedHashSet<Class<?>>();
private LinkedHashSet<String> annotatedPackages = new LinkedHashSet<String>();
private final JaxbHelper jaxbHelper;
private final ServiceRegistry serviceRegistry;
private final EntityResolver entityResolver;
private final NamingStrategy namingStrategy;
@ -82,6 +84,20 @@ public class MetadataSources {
this.jaxbHelper = new JaxbHelper( this );
this.metadataBuilder = new MetadataBuilderImpl( this );
// service registry really should be either BootstrapServiceRegistry or StandardServiceRegistry type...
if ( ! isExpectedServiceRegistryType( serviceRegistry ) ) {
LOG.debugf(
"Unexpected ServiceRegistry type [%s] encountered during building of MetadataSources; may cause " +
"problems later attempting to construct MetadataBuilder",
serviceRegistry.getClass().getName()
);
}
}
protected static boolean isExpectedServiceRegistryType(ServiceRegistry serviceRegistry) {
return BootstrapServiceRegistry.class.isInstance( serviceRegistry )
|| StandardServiceRegistry.class.isInstance( serviceRegistry );
}
public List<JaxbRoot> getJaxbRootList() {
@ -104,12 +120,37 @@ public class MetadataSources {
return namingStrategy;
}
/**
* Get a builder for metadata where non-default options can be specified.
*
* @return The built metadata.
*/
public MetadataBuilder getMetadataBuilder() {
return metadataBuilder;
return new MetadataBuilderImpl( this );
}
/**
* Get a builder for metadata where non-default options can be specified.
*
* @return The built metadata.
*/
public MetadataBuilder getMetadataBuilder(StandardServiceRegistry serviceRegistry) {
return new MetadataBuilderImpl( this, serviceRegistry );
}
/**
* Short-hand form of calling {@link #getMetadataBuilder()} and using its
* {@link org.hibernate.metamodel.MetadataBuilder#build()} method in cases where the application wants
* to accept the defaults.
*
* @return The built metadata.
*/
public Metadata buildMetadata() {
return getMetadataBuilder().buildMetadata();
return getMetadataBuilder().build();
}
public Metadata buildMetadata(StandardServiceRegistry serviceRegistry) {
return getMetadataBuilder( serviceRegistry ).build();
}
/**

View File

@ -35,5 +35,10 @@ public interface SessionFactoryBuilder {
public SessionFactoryBuilder with(EntityNotFoundDelegate entityNotFoundDelegate);
public SessionFactory buildSessionFactory();
/**
* After all options have been set, build the SessionFactory.
*
* @return The built SessionFactory.
*/
public SessionFactory build();
}

View File

@ -27,6 +27,7 @@ package org.hibernate.metamodel.source.internal;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
@ -45,11 +46,7 @@ import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.jaxb.JaxbRoot;
import org.hibernate.internal.jaxb.Origin;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbHibernateMapping;
@ -57,7 +54,10 @@ import org.hibernate.internal.jaxb.mapping.orm.JaxbEntityMappings;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.XsdException;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/**
* Helper class for unmarshalling xml configuration using StAX and JAXB.

View File

@ -25,27 +25,66 @@ package org.hibernate.metamodel.source.internal;
import javax.persistence.SharedCacheMode;
import org.hibernate.HibernateException;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.MetadataBuilder;
import org.hibernate.metamodel.MetadataSourceProcessingOrder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.jboss.logging.Logger;
/**
* @author Steve Ebersole
*/
public class MetadataBuilderImpl implements MetadataBuilder {
private static final Logger log = Logger.getLogger( MetadataBuilderImpl.class );
private final MetadataSources sources;
private final OptionsImpl options;
public MetadataBuilderImpl(MetadataSources sources) {
this(
sources,
getStandardServiceRegistry( sources.getServiceRegistry() )
);
}
private static StandardServiceRegistry getStandardServiceRegistry(ServiceRegistry serviceRegistry) {
if ( serviceRegistry == null ) {
throw new HibernateException( "ServiceRegistry passed to MetadataBuilder cannot be null" );
}
if ( StandardServiceRegistry.class.isInstance( serviceRegistry ) ) {
return ( StandardServiceRegistry ) serviceRegistry;
}
else if ( BootstrapServiceRegistry.class.isInstance( serviceRegistry ) ) {
log.debugf(
"ServiceRegistry passed to MetadataBuilder was a BootstrapServiceRegistry; this likely wont end well" +
"if attempt is made to build SessionFactory"
);
return new StandardServiceRegistryBuilder( (BootstrapServiceRegistry) serviceRegistry ).build();
}
else {
throw new HibernateException(
String.format(
"Unexpected type of ServiceRegistry [%s] encountered in attempt to build MetadataBuilder",
serviceRegistry.getClass().getName()
)
);
}
}
public MetadataBuilderImpl(MetadataSources sources, StandardServiceRegistry serviceRegistry) {
this.sources = sources;
this.options = new OptionsImpl( sources.getServiceRegistry() );
this.options = new OptionsImpl( serviceRegistry );
}
@Override
@ -79,11 +118,13 @@ public class MetadataBuilderImpl implements MetadataBuilder {
}
@Override
public Metadata buildMetadata() {
public Metadata build() {
return new MetadataImpl( sources, options );
}
private static class OptionsImpl implements Metadata.Options {
public static class OptionsImpl implements Metadata.Options {
private final StandardServiceRegistry serviceRegistry;
private MetadataSourceProcessingOrder metadataSourceProcessingOrder = MetadataSourceProcessingOrder.HBM_FIRST;
private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE;
private SharedCacheMode sharedCacheMode = SharedCacheMode.ENABLE_SELECTIVE;
@ -93,7 +134,9 @@ public class MetadataBuilderImpl implements MetadataBuilder {
private String defaultSchemaName;
private String defaultCatalogName;
public OptionsImpl(ServiceRegistry serviceRegistry) {
public OptionsImpl(StandardServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );
// cache access type
@ -152,6 +195,10 @@ public class MetadataBuilderImpl implements MetadataBuilder {
);
}
@Override
public StandardServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
@Override
public MetadataSourceProcessingOrder getMetadataSourceProcessingOrder() {

View File

@ -29,12 +29,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jboss.logging.Logger;
import org.hibernate.AssertionFailure;
import org.hibernate.DuplicateMappingException;
import org.hibernate.MappingException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.NamingStrategy;
@ -66,8 +65,8 @@ import org.hibernate.metamodel.source.annotations.AnnotationMetadataSourceProces
import org.hibernate.metamodel.source.hbm.HbmMetadataSourceProcessorImpl;
import org.hibernate.persister.spi.PersisterClassResolver;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.type.TypeResolver;
import org.jboss.logging.Logger;
/**
* Container for configuration data collected during binding the metamodel.
@ -117,7 +116,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
private boolean globallyQuotedIdentifiers = false;
public MetadataImpl(MetadataSources metadataSources, Options options) {
this.serviceRegistry = metadataSources.getServiceRegistry();
this.serviceRegistry = options.getServiceRegistry();
this.options = options;
this.identifierGeneratorFactory = serviceRegistry.getService( MutableIdentifierGeneratorFactory.class );
//new DefaultIdentifierGeneratorFactory( dialect );
@ -335,11 +334,6 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
return options;
}
@Override
public SessionFactory buildSessionFactory() {
return sessionFactoryBuilder.buildSessionFactory();
}
@Override
public ServiceRegistry getServiceRegistry() {
return serviceRegistry;
@ -461,6 +455,11 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
return sessionFactoryBuilder;
}
@Override
public SessionFactory buildSessionFactory() {
return getSessionFactoryBuilder().build();
}
@Override
public NamingStrategy getNamingStrategy() {
return options.getNamingStrategy();

View File

@ -29,6 +29,7 @@ import org.hibernate.EmptyInterceptor;
import org.hibernate.Interceptor;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.metamodel.SessionFactoryBuilder;
import org.hibernate.metamodel.source.MetadataImplementor;
@ -45,7 +46,7 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilder {
/* package-protected */
SessionFactoryBuilderImpl(MetadataImplementor metadata) {
this.metadata = metadata;
options = new SessionFactoryOptionsImpl();
options = new SessionFactoryOptionsImpl( metadata.getOptions().getServiceRegistry() );
}
@Override
@ -61,12 +62,17 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilder {
}
@Override
public SessionFactory buildSessionFactory() {
public SessionFactory build() {
return new SessionFactoryImpl(metadata, options, null );
}
private static class SessionFactoryOptionsImpl implements SessionFactory.SessionFactoryOptions {
private final StandardServiceRegistry serviceRegistry;
private Interceptor interceptor = EmptyInterceptor.INSTANCE;
public SessionFactoryOptionsImpl(StandardServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
// TODO: should there be a DefaultEntityNotFoundDelegate.INSTANCE?
private EntityNotFoundDelegate entityNotFoundDelegate = new EntityNotFoundDelegate() {
@ -75,6 +81,11 @@ public class SessionFactoryBuilderImpl implements SessionFactoryBuilder {
}
};
@Override
public StandardServiceRegistry getServiceRegistry() {
return serviceRegistry;
}
@Override
public Interceptor getInterceptor() {
return interceptor;

View File

@ -26,6 +26,7 @@ package org.hibernate.service;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceInitiator;
import org.hibernate.boot.registry.StandardServiceRegistry;
/**
* @deprecated Use {@link org.hibernate.boot.registry.StandardServiceRegistryBuilder} instead
@ -82,8 +83,7 @@ public class ServiceRegistryBuilder extends org.hibernate.boot.registry.Standard
return this;
}
@Override
public ServiceRegistry buildServiceRegistry() {
return super.buildServiceRegistry();
public StandardServiceRegistry build() {
return super.build();
}
}

View File

@ -0,0 +1,40 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.service.spi;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
/**
* Contract for contributing services.
*
* @author Steve Ebersole
*/
public interface ServiceContributor {
/**
* Contribute services to the indicated registry builder.
*
* @param serviceRegistryBuilder The builder to which services (or initiators) should be contributed.
*/
public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder);
}

View File

@ -62,7 +62,7 @@ class ManagedProviderConnectionHelper implements ConnectionHelper {
private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry();
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
}
public Connection getConnection() throws SQLException {

View File

@ -504,7 +504,7 @@ public class SchemaExport {
private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry();
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
}
public static void main(String[] args) {

View File

@ -110,7 +110,7 @@ public class SchemaUpdate {
private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry();
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
}
public static void main(String[] args) {

View File

@ -79,7 +79,7 @@ public class SchemaValidator {
private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).buildServiceRegistry();
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
}
public static void main(String[] args) {

View File

@ -64,7 +64,7 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
@Before
public void setUp() {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry();
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
}
@After

View File

@ -46,7 +46,7 @@ public class BasicCollectionBindingTests extends BaseUnitTestCase {
@Before
public void setUp() {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry();
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
}
@After
@ -68,7 +68,7 @@ public class BasicCollectionBindingTests extends BaseUnitTestCase {
MetadataSources sources = new MetadataSources( serviceRegistry );
// sources.addAnnotatedClass( EntityWithBasicCollections.class );
sources.addResource( "org/hibernate/metamodel/binding/EntityWithBasicCollections.hbm.xml" );
MetadataImpl metadata = (MetadataImpl) sources.getMetadataBuilder().with( processingOrder ).buildMetadata();
MetadataImpl metadata = (MetadataImpl) sources.getMetadataBuilder().with( processingOrder ).build();
final EntityBinding entityBinding = metadata.getEntityBinding( EntityWithBasicCollections.class.getName() );
assertNotNull( entityBinding );

View File

@ -1,7 +1,7 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
@ -23,6 +23,12 @@
*/
package org.hibernate.metamodel.source.annotations.entity;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.metamodel.MetadataBuilder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
@ -30,13 +36,6 @@ import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* @author Hardy Ferentschik
*/
@ -100,10 +99,11 @@ public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
private void createBindings() {
try {
sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
Resources resourcesAnnotation = origFrameworkMethod.getAnnotation( Resources.class );
if ( resourcesAnnotation != null ) {
sources.getMetadataBuilder().with( resourcesAnnotation.cacheMode() );
metadataBuilder.with( resourcesAnnotation.cacheMode() );
for ( Class<?> annotatedClass : resourcesAnnotation.annotatedClasses() ) {
sources.addAnnotatedClass( annotatedClass );
@ -112,7 +112,8 @@ public abstract class BaseAnnotationBindingTestCase extends BaseUnitTestCase {
sources.addResource( resourcesAnnotation.ormXmlPath() );
}
}
meta = (MetadataImpl) sources.buildMetadata();
meta = ( MetadataImpl ) metadataBuilder.build();
}
catch ( final Throwable t ) {
setupError = t;

View File

@ -149,7 +149,7 @@ public class IdentifierGeneratorTest extends BaseAnnotationBindingTestCase {
@Test
public void testUndefinedGenerator() {
try {
sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addAnnotatedClass( NamedGeneratorEntity.class );
sources.buildMetadata();
fail();

View File

@ -72,7 +72,7 @@ public class MapsIdTest extends BaseAnnotationBindingTestCase {
@Resources(annotatedClasses = DependentId.class)
public void testMapsIsOnOneToManyThrowsException() {
try {
sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addAnnotatedClass( DependentId.class );
sources.addAnnotatedClass( Dependent.class );
sources.addAnnotatedClass( Employee.class );

View File

@ -58,7 +58,7 @@ public class FetchProfileBinderTest extends BaseUnitTestCase {
@Before
public void setUp() {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry();
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
service = serviceRegistry.getService( ClassLoaderService.class );
meta = (MetadataImpl) new MetadataSources( serviceRegistry ).buildMetadata();
}

View File

@ -58,7 +58,7 @@ public class QueryBinderTest extends BaseUnitTestCase {
@Before
public void setUp() {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry();
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
service = serviceRegistry.getService( ClassLoaderService.class );
meta = (MetadataImpl) new MetadataSources( serviceRegistry ).buildMetadata();
}

View File

@ -50,7 +50,7 @@ public abstract class BaseAnnotationIndexTestCase extends BaseUnitTestCase {
@Before
public void setUp() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
meta = (MetadataImpl) sources.buildMetadata();
}

View File

@ -67,7 +67,7 @@ public class JandexHelperTest extends BaseUnitTestCase {
@Before
public void setUp() {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().buildServiceRegistry();
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().build();
classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
}

View File

@ -23,17 +23,15 @@
*/
package org.hibernate.metamodel.source.annotations.xml;
import org.junit.Test;
import static junit.framework.Assert.assertNotNull;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.MappingException;
import org.hibernate.metamodel.source.internal.MetadataImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertNotNull;
import org.junit.Test;
/**
* @author Hardy Ferentschik
@ -41,7 +39,7 @@ import static junit.framework.Assert.assertNotNull;
public class OrmXmlParserTests extends BaseUnitTestCase {
@Test
public void testSimpleOrmVersion2() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addResource( "org/hibernate/metamodel/source/annotations/xml/orm-father.xml" );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
@ -51,7 +49,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase {
@Test
public void testSimpleOrmVersion1() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addResource( "org/hibernate/metamodel/source/annotations/xml/orm-star.xml" );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
@ -61,7 +59,7 @@ public class OrmXmlParserTests extends BaseUnitTestCase {
@Test(expected = MappingException.class)
public void testInvalidOrmXmlThrowsException() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addResource( "org/hibernate/metamodel/source/annotations/xml/orm-invalid.xml" );
sources.buildMetadata();
}

View File

@ -23,9 +23,12 @@
*/
package org.hibernate.metamodel.source.internal;
import java.util.Iterator;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
@ -34,13 +37,8 @@ import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.SessionFactoryBuilder;
import org.hibernate.metamodel.binding.FetchProfile;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;
/**
* @author Hardy Ferentschik
@ -49,28 +47,28 @@ public class MetadataImplTest extends BaseUnitTestCase {
@Test(expected = IllegalArgumentException.class)
public void testAddingNullClass() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addClass( null );
sources.buildMetadata();
}
@Test(expected = IllegalArgumentException.class)
public void testAddingNullPackageName() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addPackage( null );
sources.buildMetadata();
}
@Test(expected = HibernateException.class)
public void testAddingNonExistingPackageName() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addPackage( "not.a.package" );
sources.buildMetadata();
}
@Test
public void testAddingPackageName() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addPackage( "org.hibernate.metamodel.source.internal" );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
@ -79,7 +77,7 @@ public class MetadataImplTest extends BaseUnitTestCase {
@Test
public void testAddingPackageNameWithTrailingDot() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addPackage( "org.hibernate.metamodel.source.internal." );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
@ -88,7 +86,7 @@ public class MetadataImplTest extends BaseUnitTestCase {
@Test
public void testGettingSessionFactoryBuilder() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
Metadata metadata = sources.buildMetadata();
SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();

View File

@ -62,7 +62,7 @@ public class SessionFactoryBuilderImplTest extends BaseUnitTestCase {
@Test
public void testBuildSessionFactoryWithDefaultOptions() {
SessionFactoryBuilder sessionFactoryBuilder = getSessionFactoryBuilder();
SessionFactory sessionFactory = sessionFactoryBuilder.buildSessionFactory();
SessionFactory sessionFactory = sessionFactoryBuilder.build();
assertSame( EmptyInterceptor.INSTANCE, sessionFactory.getSessionFactoryOptions().getInterceptor() );
assertTrue( EntityNotFoundDelegate.class.isInstance(
sessionFactory.getSessionFactoryOptions().getEntityNotFoundDelegate()
@ -82,14 +82,14 @@ public class SessionFactoryBuilderImplTest extends BaseUnitTestCase {
};
sessionFactoryBuilder.with( interceptor );
sessionFactoryBuilder.with( entityNotFoundDelegate );
SessionFactory sessionFactory = sessionFactoryBuilder.buildSessionFactory();
SessionFactory sessionFactory = sessionFactoryBuilder.build();
assertSame( interceptor, sessionFactory.getSessionFactoryOptions().getInterceptor() );
assertSame( entityNotFoundDelegate, sessionFactory.getSessionFactoryOptions().getEntityNotFoundDelegate() );
sessionFactory.close();
}
private SessionFactoryBuilder getSessionFactoryBuilder() {
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().buildServiceRegistry() );
MetadataSources sources = new MetadataSources( new StandardServiceRegistryBuilder().build() );
sources.addAnnotatedClass( SimpleEntity.class );
MetadataImpl metadata = (MetadataImpl) sources.buildMetadata();
return metadata.getSessionFactoryBuilder();

View File

@ -56,7 +56,7 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase {
public void testWithoutIntegrator() {
ServiceRegistry reg = new StandardServiceRegistryBuilder(new BootstrapServiceRegistryImpl())
.buildServiceRegistry();
.build();
SessionFactory sf = new Configuration()
.addAnnotatedClass( Investor.class )
@ -81,7 +81,7 @@ public class EmbeddableIntegratorTest extends BaseUnitTestCase {
public void testWithIntegrator() {
ServiceRegistry reg = new StandardServiceRegistryBuilder(
new BootstrapServiceRegistryBuilder().with( new InvestorIntegrator() ).build()
).buildServiceRegistry();
).build();
SessionFactory sf = new Configuration()
.addAnnotatedClass( Investor.class )

View File

@ -45,7 +45,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase {
cfg.addAnnotatedClass( Gate.class );
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() )
.buildServiceRegistry();
.build();
//no exception as the GoofyPersisterClassProvider is not set
SessionFactory sessionFactory = cfg.buildSessionFactory( serviceRegistry );
sessionFactory.close();
@ -54,7 +54,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase {
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() )
.addService( PersisterClassResolver.class, new GoofyPersisterClassProvider() )
.buildServiceRegistry();
.build();
cfg = new Configuration();
cfg.addAnnotatedClass( Gate.class );
try {
@ -79,7 +79,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase {
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() )
.addService( PersisterClassResolver.class, new GoofyPersisterClassProvider() )
.buildServiceRegistry();
.build();
try {
sessionFactory = cfg.buildSessionFactory( serviceRegistry );
sessionFactory.close();
@ -102,7 +102,7 @@ public class PersisterClassProviderTest extends BaseUnitTestCase {
serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() )
.addService( PersisterClassResolver.class, new GoofyPersisterClassProvider() )
.buildServiceRegistry();
.build();
try {
sessionFactory = cfg.buildSessionFactory( serviceRegistry );
sessionFactory.close();

View File

@ -32,7 +32,6 @@ import java.sql.PreparedStatement;
import java.sql.Statement;
import org.hibernate.Session;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.engine.jdbc.batch.internal.BasicBatchKey;
import org.hibernate.engine.jdbc.batch.internal.BatchBuilderImpl;
import org.hibernate.engine.jdbc.batch.internal.BatchingBatch;

View File

@ -26,7 +26,7 @@ public class ConfigurationValidationTest extends BaseUnitTestCase {
cfg.setProperty( Environment.MULTI_TENANT_CONNECTION_PROVIDER, "class.not.present.in.classpath" );
cfg.buildMappings();
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() ).buildServiceRegistry();
.applySettings( cfg.getProperties() ).build();
cfg.buildSessionFactory( serviceRegistry );
}
@ -48,7 +48,7 @@ public class ConfigurationValidationTest extends BaseUnitTestCase {
)
)
)
.buildServiceRegistry();
.build();
cfg.buildSessionFactory( serviceRegistry );
}

View File

@ -69,7 +69,7 @@ public class SchemaBasedMultiTenancyTest extends BaseUnitTestCase {
serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder()
.applySettings( cfg.getProperties() )
.addService( MultiTenantConnectionProvider.class, multiTenantConnectionProvider )
.buildServiceRegistry();
.build();
sessionFactory = (SessionFactoryImplementor) cfg.buildSessionFactory( serviceRegistry );
}

View File

@ -37,7 +37,6 @@ import org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProv
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.env.ConnectionProviderBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
@ -51,7 +50,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase {
public void testBasicBuild() {
StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() )
.buildServiceRegistry();
.build();
JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
assertTrue( jdbcServices.getDialect() instanceof H2Dialect );
@ -68,7 +67,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase {
StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( props )
.buildServiceRegistry();
.build();
JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
@ -83,7 +82,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase {
public void testBuildWithServiceOverride() {
StandardServiceRegistryImpl serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() )
.buildServiceRegistry();
.build();
JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
assertTrue( jdbcServices.getDialect() instanceof H2Dialect );
@ -95,7 +94,7 @@ public class ServiceBootstrappingTest extends BaseUnitTestCase {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( props )
.addService( ConnectionProvider.class, new UserSuppliedConnectionProviderImpl() )
.buildServiceRegistry();
.build();
jdbcServices = serviceRegistry.getService( JdbcServices.class );
assertTrue( jdbcServices.getDialect() instanceof H2Dialect );

View File

@ -0,0 +1,144 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.smoke;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.MetadataSources;
import org.junit.Test;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-7580" )
public class SessionFactoryBuildingUseCasesTest extends BaseUnitTestCase {
@Test
public void testTheSimplestUseCase() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
MetadataSources metadataSources = new MetadataSources( registry ).addAnnotatedClass( MyEntity.class );
SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory();
useSessionFactory( sessionFactory );
sessionFactory.close();
StandardServiceRegistryBuilder.destroy( registry );
}
private void useSessionFactory(SessionFactory sessionFactory) {
Session session = sessionFactory.openSession();
session.beginTransaction();
session.createQuery( "from MyEntity" ).list();
session.getTransaction().commit();
session.close();
}
@Test
public void testStillSimpleOnePhaseUseCase() {
BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build();
StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry )
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
MetadataSources metadataSources = new MetadataSources( registry ).addAnnotatedClass( MyEntity.class );
SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory();
useSessionFactory( sessionFactory );
sessionFactory.close();
StandardServiceRegistryBuilder.destroy( registry );
}
@Test
public void testSimpleTwoPhaseUseCase() {
BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build();
MetadataSources metadataSources = new MetadataSources( bootRegistry ).addAnnotatedClass( MyEntity.class );
StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry )
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
SessionFactory sessionFactory = metadataSources.buildMetadata( registry ).buildSessionFactory();
useSessionFactory( sessionFactory );
sessionFactory.close();
StandardServiceRegistryBuilder.destroy( registry );
}
@Test
public void testFullTwoPhaseUseCase() {
BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build();
MetadataSources metadataSources = new MetadataSources( bootRegistry ).addAnnotatedClass( MyEntity.class );
StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry )
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
Metadata metadata = metadataSources.getMetadataBuilder( registry ).build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); // todo rename
useSessionFactory( sessionFactory );
sessionFactory.close();
StandardServiceRegistryBuilder.destroy( registry );
}
@Test
@FailureExpected( jiraKey = "HHH-7580" )
public void testInvalidQuasiUseCase() {
BootstrapServiceRegistry bootRegistry = new BootstrapServiceRegistryBuilder().build();
MetadataSources metadataSources = new MetadataSources( bootRegistry ).addAnnotatedClass( MyEntity.class );
StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootRegistry )
.applySetting( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
.build();
SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory();
useSessionFactory( sessionFactory );
sessionFactory.close();
StandardServiceRegistryBuilder.destroy( registry );
}
@Entity( name = "MyEntity" )
@Table( name = "TST_ENT" )
public static class MyEntity {
@Id
private Long id;
}
}

View File

@ -59,7 +59,7 @@ public class TestExpectedUsage extends BaseUnitTestCase {
public void setUp() throws Exception {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( ConnectionProviderBuilder.getConnectionProviderProperties() )
.buildServiceRegistry();
.build();
}
@After

View File

@ -71,7 +71,7 @@ public class BasicDrivingTest extends BaseUnitTestCase {
TestingJtaBootstrap.prepare( configValues );
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( configValues )
.buildServiceRegistry();
.build();
}
@After

View File

@ -76,7 +76,7 @@ public class ManagedDrivingTest extends BaseUnitTestCase {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( configValues )
.buildServiceRegistry();
.build();
}
@After

View File

@ -23,16 +23,6 @@
*/
package org.hibernate.jpa.boot.internal;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.MappedSuperclass;
import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
@ -52,43 +42,47 @@ import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.MappedSuperclass;
import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;
import org.hibernate.Interceptor;
import org.hibernate.MappingException;
import org.hibernate.MappingNotFoundException;
import org.hibernate.SessionFactory;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.beanvalidation.BeanValidationIntegrator;
import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory;
import org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory;
import org.hibernate.id.factory.spi.MutableIdentifierGeneratorFactory;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.internal.jaxb.cfg.JaxbHibernateConfiguration;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.ValueHolder;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder;
import org.hibernate.jpa.boot.spi.IntegratorProvider;
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
import org.hibernate.jpa.event.spi.JpaIntegrator;
import org.hibernate.jpa.internal.EntityManagerFactoryImpl;
import org.hibernate.jpa.internal.EntityManagerMessageLogger;
import org.hibernate.jpa.event.spi.JpaIntegrator;
import org.hibernate.jpa.internal.util.LogHelper;
import org.hibernate.jpa.internal.util.PersistenceUnitTransactionTypeHelper;
import org.hibernate.jpa.packaging.internal.NativeScanner;
@ -99,12 +93,17 @@ import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.secure.internal.JACCConfiguration;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.service.ConfigLoader;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.CompositeIndex;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
/**
* @author Steve Ebersole
@ -903,7 +902,7 @@ public class EntityManagerFactoryBuilderImpl implements EntityManagerFactoryBuil
}
public ServiceRegistry buildServiceRegistry() {
return serviceRegistryBuilder.buildServiceRegistry();
return serviceRegistryBuilder.build();
}
public Configuration buildHibernateConfiguration(ServiceRegistry serviceRegistry) {

View File

@ -78,7 +78,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
private void evictOrRemoveTest() throws Exception {
Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);
@ -94,7 +94,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
cfg = createConfiguration();
regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);
@ -148,7 +148,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
private void evictOrRemoveAllTest(String configName) throws Exception {
Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);
@ -166,7 +166,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
cfg = createConfiguration();
regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);

View File

@ -27,12 +27,12 @@ import java.util.HashMap;
import java.util.Map;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cache.infinispan.InfinispanRegionFactory;
import org.hibernate.cache.infinispan.collection.CollectionRegionImpl;
import org.hibernate.cache.infinispan.entity.EntityRegionImpl;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.test.cache.infinispan.util.CacheTestUtil;
/**
@ -110,7 +110,7 @@ public class NodeEnvironment {
public void prepare() throws Exception {
serviceRegistry = (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
.build();
regionFactory = CacheTestUtil.startRegionFactory(serviceRegistry, configuration);
}

View File

@ -112,7 +112,7 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
private void putDoesNotBlockGetTest() throws Exception {
Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);
@ -212,7 +212,7 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
private void getDoesNotBlockPutTest() throws Exception {
Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);

View File

@ -81,7 +81,7 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC
public void testClearTimestampsRegionInIsolated() throws Exception {
Configuration cfg = createConfiguration();
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg,
getCacheTestSupport()
);
@ -90,7 +90,7 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC
Configuration cfg2 = createConfiguration();
InfinispanRegionFactory regionFactory2 = CacheTestUtil.startRegionFactory(
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).buildServiceRegistry(),
new StandardServiceRegistryBuilder().applySettings( cfg.getProperties() ).build(),
cfg2,
getCacheTestSupport()
);

View File

@ -41,7 +41,7 @@ public class ServiceRegistryBuilder {
public static StandardServiceRegistryImpl buildServiceRegistry(Map serviceRegistryConfig) {
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder()
.applySettings( serviceRegistryConfig )
.buildServiceRegistry();
.build();
}
public static void destroy(ServiceRegistry serviceRegistry) {

View File

@ -23,6 +23,8 @@
*/
package org.hibernate.testing.junit4;
import static org.junit.Assert.fail;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
@ -34,19 +36,20 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.Mappings;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.StringHelper;
@ -59,19 +62,14 @@ import org.hibernate.mapping.Property;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.testing.AfterClassOnce;
import org.hibernate.testing.BeforeClassOnce;
import org.hibernate.testing.OnExpectedFailure;
import org.hibernate.testing.OnFailure;
import org.hibernate.testing.SkipLog;
import org.hibernate.testing.cache.CachingRegionFactory;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
/**
* Applies functional testing logic for core Hibernate testing on top of {@link BaseUnitTestCase}
@ -126,7 +124,8 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
protected void buildSessionFactory() {
// for now, build the configuration to get all the property settings
configuration = constructAndConfigureConfiguration();
serviceRegistry = buildServiceRegistry( configuration );
BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
serviceRegistry = buildServiceRegistry( bootRegistry, configuration );
isMetadataUsed = serviceRegistry.getService( ConfigurationService.class ).getSetting(
USE_NEW_METADATA_MAPPINGS,
new ConfigurationService.Converter<Boolean>() {
@ -138,7 +137,9 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
false
);
if ( isMetadataUsed ) {
sessionFactory = ( SessionFactoryImplementor ) buildMetadata( serviceRegistry ).buildSessionFactory();
MetadataImplementor metadataImplementor = buildMetadata( bootRegistry, serviceRegistry );
afterConstructAndConfigureMetadata( metadataImplementor );
sessionFactory = ( SessionFactoryImplementor ) metadataImplementor.buildSessionFactory();
}
else {
// this is done here because Configuration does not currently support 4.0 xsd
@ -148,10 +149,24 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
afterSessionFactoryBuilt();
}
private MetadataImplementor buildMetadata(ServiceRegistry serviceRegistry) {
MetadataSources sources = new MetadataSources( serviceRegistry );
addMappings( sources );
return (MetadataImplementor) sources.buildMetadata();
protected void rebuildSessionFactory() {
if ( sessionFactory == null ) {
return;
}
buildSessionFactory();
}
protected void afterConstructAndConfigureMetadata(MetadataImplementor metadataImplementor) {
}
private MetadataImplementor buildMetadata(
BootstrapServiceRegistry bootRegistry,
StandardServiceRegistryImpl serviceRegistry) {
MetadataSources sources = new MetadataSources( bootRegistry );
addMappings( sources );
return (MetadataImplementor) sources.getMetadataBuilder( serviceRegistry ).build();
}
// TODO: is this still needed?
@ -327,20 +342,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
protected void afterConfigurationBuilt(Mappings mappings, Dialect dialect) {
}
protected StandardServiceRegistryImpl buildServiceRegistry(Configuration configuration) {
Properties properties = new Properties();
properties.putAll( configuration.getProperties() );
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
final BootstrapServiceRegistry bootstrapServiceRegistry = generateBootstrapRegistry( properties );
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder( bootstrapServiceRegistry )
.applySettings( properties );
prepareBasicRegistryBuilder( registryBuilder );
return (StandardServiceRegistryImpl) registryBuilder.buildServiceRegistry();
}
protected BootstrapServiceRegistry generateBootstrapRegistry(Properties properties) {
protected BootstrapServiceRegistry buildBootstrapServiceRegistry() {
final BootstrapServiceRegistryBuilder builder = new BootstrapServiceRegistryBuilder();
prepareBootstrapRegistryBuilder( builder );
return builder.build();
@ -349,6 +351,17 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
protected void prepareBootstrapRegistryBuilder(BootstrapServiceRegistryBuilder builder) {
}
protected StandardServiceRegistryImpl buildServiceRegistry(BootstrapServiceRegistry bootRegistry, Configuration configuration) {
Properties properties = new Properties();
properties.putAll( configuration.getProperties() );
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder( bootRegistry ).applySettings( properties );
prepareBasicRegistryBuilder( registryBuilder );
return (StandardServiceRegistryImpl) registryBuilder.build();
}
protected void prepareBasicRegistryBuilder(StandardServiceRegistryBuilder serviceRegistryBuilder) {
}
@ -396,24 +409,6 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
}
}
protected void rebuildSessionFactory() {
if ( sessionFactory == null ) {
return;
}
sessionFactory.close();
serviceRegistry.destroy();
serviceRegistry = buildServiceRegistry( configuration );
if ( isMetadataUsed ) {
// need to rebuild metadata because serviceRegistry was recreated
sessionFactory = ( SessionFactoryImplementor ) buildMetadata( serviceRegistry ).buildSessionFactory();
}
else {
sessionFactory = ( SessionFactoryImplementor ) configuration.buildSessionFactory( serviceRegistry );
}
afterSessionFactoryBuilt();
}
// before/after each test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~