Java Code Examples for org.hibernate.boot.SessionFactoryBuilder

The following examples show how to use org.hibernate.boot.SessionFactoryBuilder. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void generateSchema() {
	// This seems overkill, but building the SF is necessary to get the Integrators to kick in.
	// Metamodel will clean this up...
	try {
		SessionFactoryBuilder sfBuilder = metadata().getSessionFactoryBuilder();
		populate( sfBuilder, standardServiceRegistry );

		SchemaManagementToolCoordinator.process(
				metadata, standardServiceRegistry, configurationValues, DelayedDropRegistryNotAvailableImpl.INSTANCE
		);
	}
	catch (Exception e) {
		throw persistenceException( "Error performing schema management", e );
	}

	// release this builder
	cancel();
}
 
Example 2
Source Project: mPaaS   Source File: HibernateMetadataExtractor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SessionFactoryBuilder getSessionFactoryBuilder(
        MetadataImplementor metadata,
        SessionFactoryBuilderImplementor defaultBuilder) {
    List<String> handled = new ArrayList<>();
    for (PersistentClass entity : metadata.getEntityBindings()) {
        bind(entity, handled);
    }
    return null;
}
 
Example 3
@Override
public <T extends SessionFactoryBuilder> T unwrap(Class<T> type) {
	if ( type.isAssignableFrom( getClass() ) ) {
		return type.cast( this );
	}
	else {
		return delegate.unwrap( type );
	}
}
 
Example 4
Source Project: micronaut-sql   Source File: EntityManagerFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Builds the actual {@link SessionFactory} from the builder.
 *
 * @param sessionFactoryBuilder The {@link SessionFactoryBuilder}
 * @return The {@link SessionFactory}
 */
@Context
@Requires(beans = SessionFactoryBuilder.class)
@Bean(preDestroy = "close")
@EachBean(SessionFactoryBuilder.class)
protected SessionFactory hibernateSessionFactory(SessionFactoryBuilder sessionFactoryBuilder) {
    return sessionFactoryBuilder.build();
}
 
Example 5
@SuppressWarnings("unchecked")
public EntityManagerFactory build() {
	SessionFactoryBuilder sfBuilder = metadata().getSessionFactoryBuilder();
	populate( sfBuilder, standardServiceRegistry );

	try {
		return sfBuilder.build();
	}
	catch (Exception e) {
		throw persistenceException( "Unable to build Hibernate SessionFactory", e );
	}
}
 
Example 6
Source Project: tutorials   Source File: HibernateUtil.java    License: MIT License 5 votes vote down vote up
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addPackage("com.baeldung.hibernate.proxy");
    metadataSources.addAnnotatedClass(Company.class);
    metadataSources.addAnnotatedClass(Employee.class);

    Metadata metadata = metadataSources.buildMetadata();
    return metadata.getSessionFactoryBuilder();

}
 
Example 7
@Override
public SessionFactoryBuilder getSessionFactoryBuilder() {
	throw new UnsupportedOperationException(
			"You should not be building a SessionFactory from an in-flight metadata collector; and of course " +
					"we should better segment this in the API :)"
	);
}
 
Example 8
Source Project: lams   Source File: MetadataImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SessionFactoryBuilder getSessionFactoryBuilder() {
	final SessionFactoryBuilderImpl defaultBuilder = new SessionFactoryBuilderImpl( this, bootstrapContext );

	final ClassLoaderService cls = metadataBuildingOptions.getServiceRegistry().getService( ClassLoaderService.class );
	final java.util.Collection<SessionFactoryBuilderFactory> discoveredBuilderFactories = cls.loadJavaServices( SessionFactoryBuilderFactory.class );

	SessionFactoryBuilder builder = null;
	List<String> activeFactoryNames = null;

	for ( SessionFactoryBuilderFactory discoveredBuilderFactory : discoveredBuilderFactories ) {
		final SessionFactoryBuilder returnedBuilder = discoveredBuilderFactory.getSessionFactoryBuilder( this, defaultBuilder );
		if ( returnedBuilder != null ) {
			if ( activeFactoryNames == null ) {
				activeFactoryNames = new ArrayList<>();
			}
			activeFactoryNames.add( discoveredBuilderFactory.getClass().getName() );
			builder = returnedBuilder;
		}
	}

	if ( activeFactoryNames != null && activeFactoryNames.size() > 1 ) {
		throw new HibernateException(
				"Multiple active SessionFactoryBuilderFactory definitions were discovered : " +
						String.join(", ", activeFactoryNames)
		);
	}

	if ( builder != null ) {
		return builder;
	}

	return defaultBuilder;
}
 
Example 9
@Override
public SessionFactoryBuilder applyEntityTuplizer(
		EntityMode entityMode,
		Class<? extends EntityTuplizer> tuplizerClass) {
	this.optionsBuilder.applyEntityTuplizer( entityMode, tuplizerClass );
	return this;
}
 
Example 10
Source Project: tutorials   Source File: HibernateUtil.java    License: MIT License 5 votes vote down vote up
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addPackage("com.baeldung.hibernate.interceptors");
    metadataSources.addAnnotatedClass(User.class);

    Metadata metadata = metadataSources.buildMetadata();
    return metadata.getSessionFactoryBuilder();

}
 
Example 11
Source Project: tutorials   Source File: HibernateLifecycleUtil.java    License: MIT License 5 votes vote down vote up
private static SessionFactoryBuilder getSessionFactoryBuilder(ServiceRegistry serviceRegistry) {
    MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    metadataSources.addAnnotatedClass(FootballPlayer.class);

    Metadata metadata = metadataSources.buildMetadata();
    return metadata.getSessionFactoryBuilder();

}
 
Example 12
@Override
public SessionFactoryBuilder enableReleaseResourcesOnCloseEnabled(boolean enable) {
	this.optionsBuilder.enableReleaseResourcesOnClose( enable );
	return this;
}
 
Example 13
@Override
public SessionFactoryBuilder getSessionFactoryBuilder(MetadataImplementor metadata,
													  SessionFactoryBuilderImplementor defaultBuilder) {
	return new ReactiveSessionFactoryBuilder( metadata, defaultBuilder );
}
 
Example 14
Source Project: quarkus   Source File: PrevalidatedQuarkusMetadata.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SessionFactoryBuilder getSessionFactoryBuilder() {
    //Ensure we don't boot Hibernate using this, but rather use the #buildSessionFactoryOptionsBuilder above.
    throw new IllegalStateException("This method is not supposed to be used in Quarkus");
}
 
Example 15
Source Project: lams   Source File: Configuration.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a {@link SessionFactory} using the properties and mappings in this configuration. The
 * SessionFactory will be immutable, so changes made to this Configuration after 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();
}
 
Example 16
protected void populate(SessionFactoryBuilder sfBuilder, StandardServiceRegistry ssr) {

		final StrategySelector strategySelector = ssr.getService( StrategySelector.class );

//		// Locate and apply the requested SessionFactory-level interceptor (if one)
//		final Object sessionFactoryInterceptorSetting = configurationValues.remove( org.hibernate.cfg.AvailableSettings.INTERCEPTOR );
//		if ( sessionFactoryInterceptorSetting != null ) {
//			final Interceptor sessionFactoryInterceptor =
//					strategySelector.resolveStrategy( Interceptor.class, sessionFactoryInterceptorSetting );
//			sfBuilder.applyInterceptor( sessionFactoryInterceptor );
//		}

		// will use user override value or default to false if not supplied to follow JPA spec.
		final boolean jtaTransactionAccessEnabled = readBooleanConfigurationValue( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS );
		if ( !jtaTransactionAccessEnabled ) {
			( ( SessionFactoryBuilderImplementor ) sfBuilder ).disableJtaTransactionAccess();
		}

		final boolean allowRefreshDetachedEntity = readBooleanConfigurationValue( org.hibernate.cfg.AvailableSettings.ALLOW_REFRESH_DETACHED_ENTITY );
		if ( !allowRefreshDetachedEntity ) {
			( (SessionFactoryBuilderImplementor) sfBuilder ).disableRefreshDetachedEntity();
		}

		// Locate and apply any requested SessionFactoryObserver
		final Object sessionFactoryObserverSetting = configurationValues.remove( AvailableSettings.SESSION_FACTORY_OBSERVER );
		if ( sessionFactoryObserverSetting != null ) {
			final SessionFactoryObserver suppliedSessionFactoryObserver =
					strategySelector.resolveStrategy( SessionFactoryObserver.class, sessionFactoryObserverSetting );
			sfBuilder.addSessionFactoryObservers( suppliedSessionFactoryObserver );
		}

		sfBuilder.addSessionFactoryObservers( ServiceRegistryCloser.INSTANCE );

		sfBuilder.applyEntityNotFoundDelegate( JpaEntityNotFoundDelegate.INSTANCE );

		if ( this.validatorFactory != null ) {
			sfBuilder.applyValidatorFactory( validatorFactory );
		}
		if ( this.cdiBeanManager != null ) {
			sfBuilder.applyBeanManager( cdiBeanManager );
		}
	}
 
Example 17
public AbstractDelegatingSessionFactoryBuilder(SessionFactoryBuilder delegate) {
	this.delegate = delegate;
}
 
Example 18
@Override
public SessionFactoryBuilder applyDelayedEntityLoaderCreations(boolean delay) {
	delegate.applyDelayedEntityLoaderCreations( delay );
	return getThis();
}
 
Example 19
@Override
public SessionFactoryBuilder applyTimestampsCacheFactory(TimestampsCacheFactory factory) {
	delegate.applyTimestampsCacheFactory( factory );
	return getThis();
}
 
Example 20
@Override
public SessionFactoryBuilder applyConnectionProviderDisablesAutoCommit(boolean providerDisablesAutoCommit) {
	delegate.applyConnectionProviderDisablesAutoCommit( providerDisablesAutoCommit );
	return getThis();
}
 
Example 21
@Override
public SessionFactoryBuilder enableJpaQueryCompliance(boolean enabled) {
	delegate.enableJpaQueryCompliance( enabled );
	return getThis();
}
 
Example 22
@Override
public SessionFactoryBuilder enableJpaTransactionCompliance(boolean enabled) {
	delegate.enableJpaTransactionCompliance( enabled );
	return getThis();
}
 
Example 23
@Override
public SessionFactoryBuilder enableJpaListCompliance(boolean enabled) {
	delegate.enableJpaListCompliance( enabled );
	return getThis();
}
 
Example 24
@Override
public SessionFactoryBuilder enableJpaClosedCompliance(boolean enabled) {
	delegate.enableJpaClosedCompliance( enabled );
	return getThis();
}
 
Example 25
@Override
@SuppressWarnings("unchecked")
public <S extends SessionFactoryBuilder> S unwrap(Class<S> type) {
	return (S) this;
}
 
Example 26
@Override
public SessionFactoryBuilder getSessionFactoryBuilder() {
	return delegate.getSessionFactoryBuilder();
}
 
Example 27
@Override
public SessionFactoryBuilder applyBeanManager(Object beanManager) {
	this.optionsBuilder.applyBeanManager(  beanManager );
	return this;
}
 
Example 28
@Override
public SessionFactoryBuilder applyValidatorFactory(Object validatorFactory) {
	this.optionsBuilder.applyValidatorFactory( validatorFactory );
	return this;
}
 
Example 29
@Override
public SessionFactoryBuilder applyName(String sessionFactoryName) {
	this.optionsBuilder.applySessionFactoryName( sessionFactoryName );
	return this;
}
 
Example 30
@Override
public SessionFactoryBuilder applyNameAsJndiName(boolean isJndiName) {
	this.optionsBuilder.enableSessionFactoryNameAsJndiName( isJndiName );
	return this;
}