Java Code Examples for org.hibernate.boot.MetadataBuilder#build()

The following examples show how to use org.hibernate.boot.MetadataBuilder#build() . 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
Source File: DefaultPersistManager.java    From onedev with MIT License 5 votes vote down vote up
protected Metadata buildMetadata() {
	MetadataSources metadataSources = new MetadataSources(serviceRegistry);
	for (Class<? extends AbstractEntity> each: ClassUtils.findImplementations(AbstractEntity.class, AbstractEntity.class)) {
		metadataSources.addAnnotatedClass(each);
	}
	
	MetadataBuilder builder = metadataSources.getMetadataBuilder();
	builder.applyPhysicalNamingStrategy(physicalNamingStrategy);
	return builder.build();
}
 
Example 2
Source File: AbstractTest.java    From hypersistence-optimizer with Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
        .enableAutoClose();

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
        .applySettings(properties())
        .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
 
Example 3
Source File: Configuration.java    From lams with 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 4
Source File: AbstractTest.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator(integrator);
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
            .applySettings(properties())
            .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);
    metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        additionalTypes.stream().forEach(type -> {
            metadataBuilder.applyTypes((typeContributions, serviceRegistry1) -> {
                if(type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType ){
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
 
Example 5
Source File: AbstractTest.java    From hibernate-types with Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator(integrator);
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
            .applySettings(properties())
            .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);
    metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
 
Example 6
Source File: AbstractTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
        .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator( integrator );
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
        .applySettings(properties())
        .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder()
    .enableNewIdentifierGeneratorSupport(true)
    .applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        additionalTypes.stream().forEach(type -> {
            metadataBuilder.applyTypes((typeContributions, sr) -> {
                if(type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType ){
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }

    additionalMetadata(metadataBuilder);

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if(interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}