Search in sources :

Example 1 with BootstrapServiceRegistry

use of org.hibernate.boot.registry.BootstrapServiceRegistry 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 BootstrapServiceRegistry

use of org.hibernate.boot.registry.BootstrapServiceRegistry in project hibernate-orm by hibernate.

the class BaseCoreFunctionalTestCase method buildSessionFactory.

protected void buildSessionFactory(Consumer<Configuration> configurationAdapter) {
    // for now, build the configuration to get all the property settings
    configuration = constructAndConfigureConfiguration();
    if (configurationAdapter != null) {
        configurationAdapter.accept(configuration);
    }
    BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
    serviceRegistry = buildServiceRegistry(bootRegistry, configuration);
    // this is done here because Configuration does not currently support 4.0 xsd
    afterConstructAndConfigureConfiguration(configuration);
    sessionFactory = (SessionFactoryImplementor) configuration.buildSessionFactory(serviceRegistry);
    afterSessionFactoryBuilt();
}
Also used : BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry)

Example 3 with BootstrapServiceRegistry

use of org.hibernate.boot.registry.BootstrapServiceRegistry in project hibernate-orm by hibernate.

the class ServiceRegistryClosingCascadeTest method testSessionFactoryClosing.

@Test
public void testSessionFactoryClosing() {
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    StandardServiceRegistry sr = new StandardServiceRegistryBuilder(bsr).build();
    assertTrue(((BootstrapServiceRegistryImpl) bsr).isActive());
    Configuration config = new Configuration();
    SessionFactory sf = config.buildSessionFactory(sr);
    sf.close();
    assertFalse(((BootstrapServiceRegistryImpl) bsr).isActive());
}
Also used : SessionFactory(org.hibernate.SessionFactory) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 4 with BootstrapServiceRegistry

use of org.hibernate.boot.registry.BootstrapServiceRegistry in project hibernate-orm by hibernate.

the class FetchProfileTest method testWrongClass.

@Test
public void testWrongClass() {
    final MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(Customer3.class).addAnnotatedClass(Order.class).addAnnotatedClass(Country.class);
    try {
        metadataSources.buildMetadata();
        fail();
    } catch (MappingException e) {
        log.trace("success");
    } finally {
        ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
        if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
            BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Example 5 with BootstrapServiceRegistry

use of org.hibernate.boot.registry.BootstrapServiceRegistry in project hibernate-orm by hibernate.

the class FetchProfileTest method testUnsupportedFetchMode.

@Test
public void testUnsupportedFetchMode() {
    final MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(Customer4.class).addAnnotatedClass(Order.class).addAnnotatedClass(Country.class);
    try {
        metadataSources.buildMetadata();
        fail();
    } catch (MappingException e) {
        log.trace("success");
    } finally {
        ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
        if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
            BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Aggregations

BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)47 Test (org.junit.Test)33 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)31 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)30 MetadataSources (org.hibernate.boot.MetadataSources)28 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)21 ServiceRegistry (org.hibernate.service.ServiceRegistry)15 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)13 SeContainer (javax.enterprise.inject.se.SeContainer)8 SeContainerInitializer (javax.enterprise.inject.se.SeContainerInitializer)6 Metadata (org.hibernate.boot.Metadata)5 TheEntity (org.hibernate.test.cdi.events.TheEntity)5 Properties (java.util.Properties)4 MappingException (org.hibernate.MappingException)4 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)4 TestForIssue (org.hibernate.testing.TestForIssue)4 FileInputStream (java.io.FileInputStream)3 AnnotationException (org.hibernate.AnnotationException)3 RegionFactory (org.hibernate.cache.spi.RegionFactory)3 Configuration (org.hibernate.cfg.Configuration)3