Search in sources :

Example 6 with SessionFactoryBuilder

use of org.hibernate.boot.SessionFactoryBuilder in project hibernate-orm by hibernate.

the class AbstractMultiTenancyTest method sessionFactory.

protected SessionFactory sessionFactory(Map<String, Object> settings) {
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    for (Class annotatedClasses : getAnnotatedClasses()) {
        metadataSources.addAnnotatedClass(annotatedClasses);
    }
    Metadata metadata = metadataSources.buildMetadata();
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    final GenerationTargetToDatabase frontEndSchemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProviderMap.get(FRONT_END_TENANT)));
    final GenerationTargetToDatabase backEndSchemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProviderMap.get(BACK_END_TENANT)));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, frontEndSchemaGenerator, backEndSchemaGenerator);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, frontEndSchemaGenerator, backEndSchemaGenerator);
    final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
    return sessionFactoryBuilder.build();
}
Also used : DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)

Example 7 with SessionFactoryBuilder

use of org.hibernate.boot.SessionFactoryBuilder in project hibernate-orm by hibernate.

the class Configuration method buildSessionFactory.

/**
	 * Create a {@link SessionFactory} using the properties and mappings in this configuration. The
	 * SessionFactory will be immutable, so changes made to this Configuration afterQuery building the
	 * SessionFactory will not affect it.
	 *
	 * @param serviceRegistry The registry of services to be used in creating this session factory.
	 *
	 * @return The built {@link SessionFactory}
	 *
	 * @throws HibernateException usually indicates an invalid configuration or invalid mapping information
	 */
public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throws HibernateException {
    log.debug("Building session factory using provided StandardServiceRegistry");
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder((StandardServiceRegistry) serviceRegistry);
    if (implicitNamingStrategy != null) {
        metadataBuilder.applyImplicitNamingStrategy(implicitNamingStrategy);
    }
    if (physicalNamingStrategy != null) {
        metadataBuilder.applyPhysicalNamingStrategy(physicalNamingStrategy);
    }
    if (sharedCacheMode != null) {
        metadataBuilder.applySharedCacheMode(sharedCacheMode);
    }
    if (!typeContributorRegistrations.isEmpty()) {
        for (TypeContributor typeContributor : typeContributorRegistrations) {
            metadataBuilder.applyTypes(typeContributor);
        }
    }
    if (!basicTypes.isEmpty()) {
        for (BasicType basicType : basicTypes) {
            metadataBuilder.applyBasicType(basicType);
        }
    }
    if (sqlFunctions != null) {
        for (Map.Entry<String, SQLFunction> entry : sqlFunctions.entrySet()) {
            metadataBuilder.applySqlFunction(entry.getKey(), entry.getValue());
        }
    }
    if (auxiliaryDatabaseObjectList != null) {
        for (AuxiliaryDatabaseObject auxiliaryDatabaseObject : auxiliaryDatabaseObjectList) {
            metadataBuilder.applyAuxiliaryDatabaseObject(auxiliaryDatabaseObject);
        }
    }
    if (attributeConverterDefinitionsByClass != null) {
        for (AttributeConverterDefinition attributeConverterDefinition : attributeConverterDefinitionsByClass.values()) {
            metadataBuilder.applyAttributeConverter(attributeConverterDefinition);
        }
    }
    final Metadata metadata = metadataBuilder.build();
    final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
    if (interceptor != null && interceptor != EmptyInterceptor.INSTANCE) {
        sessionFactoryBuilder.applyInterceptor(interceptor);
    }
    if (getSessionFactoryObserver() != null) {
        sessionFactoryBuilder.addSessionFactoryObservers(getSessionFactoryObserver());
    }
    if (getEntityNotFoundDelegate() != null) {
        sessionFactoryBuilder.applyEntityNotFoundDelegate(getEntityNotFoundDelegate());
    }
    if (getEntityTuplizerFactory() != null) {
        sessionFactoryBuilder.applyEntityTuplizerFactory(getEntityTuplizerFactory());
    }
    if (getCurrentTenantIdentifierResolver() != null) {
        sessionFactoryBuilder.applyCurrentTenantIdentifierResolver(getCurrentTenantIdentifierResolver());
    }
    return sessionFactoryBuilder.build();
}
Also used : BasicType(org.hibernate.type.BasicType) MetadataBuilder(org.hibernate.boot.MetadataBuilder) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) Metadata(org.hibernate.boot.Metadata) SQLFunction(org.hibernate.dialect.function.SQLFunction) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Map(java.util.Map) HashMap(java.util.HashMap) TypeContributor(org.hibernate.boot.model.TypeContributor)

Aggregations

SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)7 Metadata (org.hibernate.boot.Metadata)3 MetadataSources (org.hibernate.boot.MetadataSources)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 PersistenceException (javax.persistence.PersistenceException)2 MetadataBuilder (org.hibernate.boot.MetadataBuilder)2 DdlTransactionIsolatorTestingImpl (org.hibernate.test.util.DdlTransactionIsolatorTestingImpl)2 HibernateSchemaManagementTool (org.hibernate.tool.schema.internal.HibernateSchemaManagementTool)2 SchemaCreatorImpl (org.hibernate.tool.schema.internal.SchemaCreatorImpl)2 SchemaDropperImpl (org.hibernate.tool.schema.internal.SchemaDropperImpl)2 GenerationTargetToDatabase (org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase)2 HibernateException (org.hibernate.HibernateException)1 TypeContributor (org.hibernate.boot.model.TypeContributor)1 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 SessionFactoryBuilderFactory (org.hibernate.boot.spi.SessionFactoryBuilderFactory)1 SQLFunction (org.hibernate.dialect.function.SQLFunction)1