Search in sources :

Example 1 with TypeContributor

use of org.hibernate.boot.model.TypeContributor in project hibernate-orm by hibernate.

the class OsgiSessionFactoryService method buildSessionFactory.

private Object buildSessionFactory(Bundle requestingBundle, OsgiClassLoader osgiClassLoader) {
    final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
    bsrBuilder.applyClassLoaderService(new OSGiClassLoaderServiceImpl(osgiClassLoader, osgiServiceUtil));
    final Integrator[] integrators = osgiServiceUtil.getServiceImpls(Integrator.class);
    for (Integrator integrator : integrators) {
        bsrBuilder.applyIntegrator(integrator);
    }
    final StrategyRegistrationProvider[] strategyRegistrationProviders = osgiServiceUtil.getServiceImpls(StrategyRegistrationProvider.class);
    for (StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders) {
        bsrBuilder.applyStrategySelectors(strategyRegistrationProvider);
    }
    final BootstrapServiceRegistry bsr = bsrBuilder.build();
    final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
    // Allow bundles to put the config file somewhere other than the root level.
    final BundleWiring bundleWiring = (BundleWiring) requestingBundle.adapt(BundleWiring.class);
    final Collection<String> cfgResources = bundleWiring.listResources("/", "hibernate.cfg.xml", BundleWiring.LISTRESOURCES_RECURSE);
    if (cfgResources.size() == 0) {
        ssrBuilder.configure();
    } else {
        if (cfgResources.size() > 1) {
            LOG.warn("Multiple hibernate.cfg.xml files found in the persistence bundle.  Using the first one discovered.");
        }
        String cfgResource = "/" + cfgResources.iterator().next();
        ssrBuilder.configure(cfgResource);
    }
    ssrBuilder.applySetting(AvailableSettings.JTA_PLATFORM, osgiJtaPlatform);
    final StandardServiceRegistry ssr = ssrBuilder.build();
    final MetadataBuilder metadataBuilder = new MetadataSources(ssr).getMetadataBuilder();
    final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls(TypeContributor.class);
    for (TypeContributor typeContributor : typeContributors) {
        metadataBuilder.applyTypes(typeContributor);
    }
    return metadataBuilder.build().buildSessionFactory();
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) BundleWiring(org.osgi.framework.wiring.BundleWiring) MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) TypeContributor(org.hibernate.boot.model.TypeContributor) StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) Integrator(org.hibernate.integrator.spi.Integrator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 2 with TypeContributor

use of org.hibernate.boot.model.TypeContributor in project hibernate-orm by hibernate.

the class MetadataBuildingProcess method handleTypes.

//	private static JandexInitManager buildJandexInitializer(
//			MetadataBuildingOptions options,
//			ClassLoaderAccess classLoaderAccess) {
//		final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
//				org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
//				options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
//				false
//		);
//
//		return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
//	}
private static BasicTypeRegistry handleTypes(MetadataBuildingOptions options) {
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    // ultimately this needs to change a little bit to account for HHH-7792
    final BasicTypeRegistry basicTypeRegistry = new BasicTypeRegistry();
    final TypeContributions typeContributions = new TypeContributions() {

        @Override
        public void contributeType(org.hibernate.type.BasicType type) {
            basicTypeRegistry.register(type);
        }

        @Override
        public void contributeType(BasicType type, String... keys) {
            basicTypeRegistry.register(type, keys);
        }

        @Override
        public void contributeType(UserType type, String[] keys) {
            basicTypeRegistry.register(type, keys);
        }

        @Override
        public void contributeType(CompositeUserType type, String[] keys) {
            basicTypeRegistry.register(type, keys);
        }
    };
    // add Dialect contributed types
    final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
    dialect.contributeTypes(typeContributions, options.getServiceRegistry());
    // add TypeContributor contributed types.
    for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
        contributor.contribute(typeContributions, options.getServiceRegistry());
    }
    // add explicit application registered types
    for (BasicTypeRegistration basicTypeRegistration : options.getBasicTypeRegistrations()) {
        basicTypeRegistry.register(basicTypeRegistration.getBasicType(), basicTypeRegistration.getRegistrationKeys());
    }
    return basicTypeRegistry;
}
Also used : BasicType(org.hibernate.type.BasicType) TypeContributions(org.hibernate.boot.model.TypeContributions) Dialect(org.hibernate.dialect.Dialect) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) BasicTypeRegistration(org.hibernate.boot.spi.BasicTypeRegistration) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) CompositeUserType(org.hibernate.usertype.CompositeUserType) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) TypeContributor(org.hibernate.boot.model.TypeContributor)

Example 3 with TypeContributor

use of org.hibernate.boot.model.TypeContributor in project hibernate-orm by hibernate.

the class EntityManagerFactoryBuilderImpl method populate.

protected void populate(MetadataBuilder metamodelBuilder, MergedSettings mergedSettings, StandardServiceRegistry ssr, List<AttributeConverterDefinition> attributeConverterDefinitions) {
    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) {
        for (CacheRegionDefinition localCacheRegionDefinition : mergedSettings.cacheRegionDefinitions) {
            metamodelBuilder.applyCacheRegionDefinition(localCacheRegionDefinition);
        }
    }
    final TypeContributorList typeContributorList = (TypeContributorList) configurationValues.remove(TYPE_CONTRIBUTORS);
    if (typeContributorList != null) {
        for (TypeContributor typeContributor : typeContributorList.getTypeContributors()) {
            metamodelBuilder.applyTypes(typeContributor);
        }
    }
    if (attributeConverterDefinitions != null) {
        for (AttributeConverterDefinition attributeConverterDefinition : attributeConverterDefinitions) {
            metamodelBuilder.applyAttributeConverter(attributeConverterDefinition);
        }
    }
}
Also used : AttributeConverterDefinition(org.hibernate.cfg.AttributeConverterDefinition) CacheRegionDefinition(org.hibernate.boot.CacheRegionDefinition) TypeContributorList(org.hibernate.jpa.boot.spi.TypeContributorList) StandardScanOptions(org.hibernate.boot.archive.scan.internal.StandardScanOptions) TypeContributor(org.hibernate.boot.model.TypeContributor)

Example 4 with TypeContributor

use of org.hibernate.boot.model.TypeContributor 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)

Example 5 with TypeContributor

use of org.hibernate.boot.model.TypeContributor in project hibernate-orm by hibernate.

the class OsgiPersistenceProvider method generateSettings.

@SuppressWarnings("unchecked")
private Map generateSettings(Map properties) {
    final Map settings = new HashMap();
    if (properties != null) {
        settings.putAll(properties);
    }
    settings.put(AvailableSettings.JTA_PLATFORM, osgiJtaPlatform);
    final Integrator[] integrators = osgiServiceUtil.getServiceImpls(Integrator.class);
    final IntegratorProvider integratorProvider = new IntegratorProvider() {

        @Override
        public List<Integrator> getIntegrators() {
            return Arrays.asList(integrators);
        }
    };
    settings.put(EntityManagerFactoryBuilderImpl.INTEGRATOR_PROVIDER, integratorProvider);
    final StrategyRegistrationProvider[] strategyRegistrationProviders = osgiServiceUtil.getServiceImpls(StrategyRegistrationProvider.class);
    final StrategyRegistrationProviderList strategyRegistrationProviderList = new StrategyRegistrationProviderList() {

        @Override
        public List<StrategyRegistrationProvider> getStrategyRegistrationProviders() {
            return Arrays.asList(strategyRegistrationProviders);
        }
    };
    settings.put(EntityManagerFactoryBuilderImpl.STRATEGY_REGISTRATION_PROVIDERS, strategyRegistrationProviderList);
    final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls(TypeContributor.class);
    final TypeContributorList typeContributorList = new TypeContributorList() {

        @Override
        public List<TypeContributor> getTypeContributors() {
            return Arrays.asList(typeContributors);
        }
    };
    settings.put(EntityManagerFactoryBuilderImpl.TYPE_CONTRIBUTORS, typeContributorList);
    return settings;
}
Also used : StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) HashMap(java.util.HashMap) Integrator(org.hibernate.integrator.spi.Integrator) TypeContributorList(org.hibernate.jpa.boot.spi.TypeContributorList) HashMap(java.util.HashMap) Map(java.util.Map) IntegratorProvider(org.hibernate.jpa.boot.spi.IntegratorProvider) StrategyRegistrationProviderList(org.hibernate.jpa.boot.spi.StrategyRegistrationProviderList) TypeContributor(org.hibernate.boot.model.TypeContributor)

Aggregations

TypeContributor (org.hibernate.boot.model.TypeContributor)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MetadataBuilder (org.hibernate.boot.MetadataBuilder)2 StrategyRegistrationProvider (org.hibernate.boot.registry.selector.StrategyRegistrationProvider)2 Integrator (org.hibernate.integrator.spi.Integrator)2 TypeContributorList (org.hibernate.jpa.boot.spi.TypeContributorList)2 BasicType (org.hibernate.type.BasicType)2 CacheRegionDefinition (org.hibernate.boot.CacheRegionDefinition)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)1 StandardScanOptions (org.hibernate.boot.archive.scan.internal.StandardScanOptions)1 TypeContributions (org.hibernate.boot.model.TypeContributions)1 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)1 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)1 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1