Java Code Examples for org.hibernate.boot.MetadataBuilder

The following examples show how to use org.hibernate.boot.MetadataBuilder. 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
Source Project: quarkus   Source File: FastBootMetadataBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Greatly simplified copy of
 * org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl#populate(org.hibernate.boot.MetadataBuilder,
 * org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.MergedSettings,
 * org.hibernate.boot.registry.StandardServiceRegistry, java.util.List)
 */
protected void populate(MetadataBuilder metamodelBuilder, List<CacheRegionDefinition> cacheRegionDefinitions,
        StandardServiceRegistry ssr) {

    ((MetadataBuilderImplementor) metamodelBuilder).getBootstrapContext().markAsJpaBootstrap();

    metamodelBuilder.applyScanEnvironment(new StandardJpaScanEnvironmentImpl(persistenceUnit));
    metamodelBuilder.applyScanOptions(new StandardScanOptions(
            (String) buildTimeSettings.get(org.hibernate.cfg.AvailableSettings.SCANNER_DISCOVERY),
            persistenceUnit.isExcludeUnlistedClasses()));

    if (cacheRegionDefinitions != null) {
        cacheRegionDefinitions.forEach(metamodelBuilder::applyCacheRegionDefinition);
    }

    final TypeContributorList typeContributorList = (TypeContributorList) buildTimeSettings
            .get(EntityManagerFactoryBuilderImpl.TYPE_CONTRIBUTORS);
    if (typeContributorList != null) {
        typeContributorList.getTypeContributors().forEach(metamodelBuilder::applyTypes);
    }
}
 
Example 2
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MetadataBuilder applyAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass) {
	this.bootstrapContext.addAttributeConverterInfo(
			new AttributeConverterInfo() {
				@Override
				public Class<? extends AttributeConverter> getConverterClass() {
					return attributeConverterClass;
				}

				@Override
				public ConverterDescriptor toConverterDescriptor(MetadataBuildingContext context) {
					return new ClassBasedConverterDescriptor(
							attributeConverterClass,
							null,
							context.getBootstrapContext().getClassmateContext()
					);
				}
			}
	);
	return this;
}
 
Example 3
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MetadataBuilder applyAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass, boolean autoApply) {
	this.bootstrapContext.addAttributeConverterInfo(
			new AttributeConverterInfo() {
				@Override
				public Class<? extends AttributeConverter> getConverterClass() {
					return attributeConverterClass;
				}

				@Override
				public ConverterDescriptor toConverterDescriptor(MetadataBuildingContext context) {
					return new ClassBasedConverterDescriptor(
							attributeConverterClass,
							autoApply,
							context.getBootstrapContext().getClassmateContext()
					);
				}
			}
	);
	return this;
}
 
Example 4
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MetadataBuilder applyAttributeConverter(AttributeConverter attributeConverter) {
	this.bootstrapContext.addAttributeConverterInfo(
			new AttributeConverterInfo() {
				@Override
				public Class<? extends AttributeConverter> getConverterClass() {
					return attributeConverter.getClass();
				}

				@Override
				public ConverterDescriptor toConverterDescriptor(MetadataBuildingContext context) {
					return new InstanceBasedConverterDescriptor(
							attributeConverter,
							null,
							context.getBootstrapContext().getClassmateContext()
					);
				}
			}
	);
	return this;
}
 
Example 5
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public MetadataBuilder applyAttributeConverter(AttributeConverter attributeConverter, boolean autoApply) {
	this.bootstrapContext.addAttributeConverterInfo(
			new AttributeConverterInfo() {
				@Override
				public Class<? extends AttributeConverter> getConverterClass() {
					return attributeConverter.getClass();
				}

				@Override
				public ConverterDescriptor toConverterDescriptor(MetadataBuildingContext context) {
					return new InstanceBasedConverterDescriptor(
							attributeConverter,
							autoApply,
							context.getBootstrapContext().getClassmateContext()
					);
				}
			}
	);
	return this;
}
 
Example 6
@Override
protected void initialize(MetadataBuilder metadataBuilder) {
	super.initialize( metadataBuilder );
	//tag::embeddable-multiple-ImplicitNamingStrategyComponentPathImpl[]
	metadataBuilder.applyImplicitNamingStrategy(
		ImplicitNamingStrategyComponentPathImpl.INSTANCE
	);
	//end::embeddable-multiple-ImplicitNamingStrategyComponentPathImpl[]
}
 
Example 7
@Override
public void contribute(MetadataBuilder metadataBuilder, StandardServiceRegistry serviceRegistry) {
    DialectResolver dialectResolver = serviceRegistry.getService(DialectResolver.class);

    if (!(dialectResolver instanceof DialectResolverSet)) {
        logger.warnf("DialectResolver '%s' is not an instance of DialectResolverSet, not registering SQLiteDialect",
                dialectResolver);
        return;
    }

    ((DialectResolverSet) dialectResolver).addResolver(resolver);
}
 
Example 8
Source Project: onedev   Source File: DefaultPersistManager.java    License: 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 9
Source Project: lams   Source File: SchemaValidatorTask.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void configure(MetadataBuilder metadataBuilder, StandardServiceRegistry serviceRegistry) {
	final StrategySelector strategySelector = serviceRegistry.getService( StrategySelector.class );
	if ( implicitNamingStrategy != null ) {
		metadataBuilder.applyImplicitNamingStrategy(
				strategySelector.resolveStrategy( ImplicitNamingStrategy.class, implicitNamingStrategy )
		);
	}
	if ( physicalNamingStrategy != null ) {
		metadataBuilder.applyPhysicalNamingStrategy(
				strategySelector.resolveStrategy( PhysicalNamingStrategy.class, physicalNamingStrategy )
		);
	}
}
 
Example 10
protected void populate(
		MetadataBuilder metamodelBuilder,
		MergedSettings mergedSettings,
		StandardServiceRegistry ssr,
		List<AttributeConverterDefinition> attributeConverterDefinitions) {
	( (MetadataBuilderImplementor) metamodelBuilder ).getBootstrapContext().markAsJpaBootstrap();

	if ( persistenceUnit.getTempClassLoader() != null ) {
		metamodelBuilder.applyTempClassLoader( persistenceUnit.getTempClassLoader() );
	}

	metamodelBuilder.applyScanEnvironment( new StandardJpaScanEnvironmentImpl( persistenceUnit ) );
	metamodelBuilder.applyScanOptions(
			new StandardScanOptions(
					(String) configurationValues.get( org.hibernate.cfg.AvailableSettings.SCANNER_DISCOVERY ),
					persistenceUnit.isExcludeUnlistedClasses()
			)
	);

	if ( mergedSettings.cacheRegionDefinitions != null ) {
		mergedSettings.cacheRegionDefinitions.forEach( metamodelBuilder::applyCacheRegionDefinition );
	}

	final TypeContributorList typeContributorList = (TypeContributorList) configurationValues.remove(
			TYPE_CONTRIBUTORS
	);
	if ( typeContributorList != null ) {
		typeContributorList.getTypeContributors().forEach( metamodelBuilder::applyTypes );
	}

	if ( attributeConverterDefinitions != null ) {
		attributeConverterDefinitions.forEach( metamodelBuilder::applyAttributeConverter );
	}
}
 
Example 11
Source Project: wallride   Source File: Hbm2ddl.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	String locationPattern = "classpath:/org/wallride/domain/*";

	final BootstrapServiceRegistry registry = new BootstrapServiceRegistryBuilder().build();
	final MetadataSources metadataSources = new MetadataSources(registry);
	final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(registry);

	registryBuilder.applySetting(AvailableSettings.DIALECT, ExtendedMySQL5InnoDBDialect.class.getCanonicalName());
	registryBuilder.applySetting(AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, true);
	registryBuilder.applySetting(AvailableSettings.PHYSICAL_NAMING_STRATEGY, PhysicalNamingStrategySnakeCaseImpl.class);

	final PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
	final Resource[] resources = resourcePatternResolver.getResources(locationPattern);
	final SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
	for (Resource resource : resources) {
		MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
		AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
		if (metadata.hasAnnotation(Entity.class.getName())) {
			metadataSources.addAnnotatedClass(Class.forName(metadata.getClassName()));
		}
	}

	final StandardServiceRegistryImpl registryImpl = (StandardServiceRegistryImpl) registryBuilder.build();
	final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(registryImpl);

	new SchemaExport()
			.setHaltOnError(true)
			.setDelimiter(";")
			.create(EnumSet.of(TargetType.STDOUT), metadataBuilder.build());
}
 
Example 12
@Override
public void contribute(MetadataBuilder metadataBuilder) {
    metadataBuilder.applySqlFunction(
        "group_concat",
        new StandardSQLFunction("group_concat", StandardBasicTypes.STRING)
    );
}
 
Example 13
Source Project: lams   Source File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public MetadataBuilder enableNewIdentifierGeneratorSupport(boolean enabled) {
	if ( enabled ) {
		this.options.idGenerationTypeInterpreter.disableLegacyFallback();
	}
	else {
		this.options.idGenerationTypeInterpreter.enableLegacyFallback();
	}
	return this;
}
 
Example 14
@Override
public void contribute(MetadataBuilder metadataBuilder) {
    metadataBuilder.applySqlFunction(
            "date_trunc",
            new SQLFunctionTemplate(
                    StandardBasicTypes.TIMESTAMP,
                    "date_trunc('day', (?1 AT TIME ZONE ?2))"
            )
    );
}
 
Example 15
Source Project: hypersistence-optimizer   Source File: AbstractTest.java    License: 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 16
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 17
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();
}
 
Example 18
@Override
public MetadataBuilder applyImplicitSchemaName(String implicitSchemaName) {
	delegate.applyImplicitSchemaName( implicitSchemaName );
	return getThis();
}
 
Example 19
@Override
public MetadataBuilder applyImplicitCatalogName(String implicitCatalogName) {
	delegate.applyImplicitCatalogName( implicitCatalogName );
	return getThis();
}
 
Example 20
@Override
public MetadataBuilder applyImplicitNamingStrategy(ImplicitNamingStrategy namingStrategy) {
	delegate.applyImplicitNamingStrategy( namingStrategy );
	return getThis();
}
 
Example 21
@Override
public MetadataBuilder applyPhysicalNamingStrategy(PhysicalNamingStrategy namingStrategy) {
	delegate.applyPhysicalNamingStrategy( namingStrategy );
	return getThis();
}
 
Example 22
@Override
public MetadataBuilder applySharedCacheMode(SharedCacheMode cacheMode) {
	delegate.applySharedCacheMode( cacheMode );
	return getThis();
}
 
Example 23
@Override
public MetadataBuilder applyAccessType(AccessType accessType) {
	delegate.applyAccessType( accessType );
	return getThis();
}
 
Example 24
@Override
public MetadataBuilder applyIndexView(IndexView jandexView) {
	delegate.applyIndexView( jandexView );
	return getThis();
}
 
Example 25
@Override
public MetadataBuilder applyScanOptions(ScanOptions scanOptions) {
	delegate.applyScanOptions( scanOptions );
	return getThis();
}
 
Example 26
@Override
public MetadataBuilder applyScanEnvironment(ScanEnvironment scanEnvironment) {
	delegate.applyScanEnvironment( scanEnvironment );
	return getThis();
}
 
Example 27
@Override
public MetadataBuilder applyScanner(Scanner scanner) {
	delegate.applyScanner( scanner );
	return getThis();
}
 
Example 28
@Override
public MetadataBuilder applyArchiveDescriptorFactory(ArchiveDescriptorFactory factory) {
	delegate.applyArchiveDescriptorFactory( factory );
	return getThis();
}
 
Example 29
@Override
public MetadataBuilder enableNewIdentifierGeneratorSupport(boolean enable) {
	delegate.enableNewIdentifierGeneratorSupport( enable );
	return getThis();
}
 
Example 30
@Override
public MetadataBuilder enableExplicitDiscriminatorsForJoinedSubclassSupport(boolean enabled) {
	delegate.enableExplicitDiscriminatorsForJoinedSubclassSupport( enabled );
	return getThis();
}