Search in sources :

Example 1 with BootstrapServiceRegistryBuilder

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

use of org.hibernate.boot.registry.BootstrapServiceRegistryBuilder 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 3 with BootstrapServiceRegistryBuilder

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

the class CfgXmlResourceNameClosingTest method testStreamClosing.

@Test
public void testStreamClosing() {
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().applyClassLoaderService(classLoaderService).build();
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).configure("org/hibernate/test/boot/cfgXml/hibernate.cfg.xml").build();
    try {
        for (InputStreamWrapper openedStream : classLoaderService.openedStreams) {
            assertTrue(openedStream.wasClosed);
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
    assertTrue(classLoaderService.stopped);
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 4 with BootstrapServiceRegistryBuilder

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

the class SchemaUpdateSchemaNameTest method cleanup.

@After
public void cleanup() {
    // Drops the table after the sql alter test.
    StandardServiceRegistry ssr = null;
    try {
        // build simple configuration
        final Configuration cfg = buildConfiguration(SimpleFirst.class);
        // Build Standard Service Registry
        ssr = new StandardServiceRegistryBuilder(new BootstrapServiceRegistryBuilder().build(), cfg.getStandardServiceRegistryBuilder().getAggregatedCfgXml()).applySettings(cfg.getProperties()).build();
        SessionFactory sf = cfg.buildSessionFactory(ssr);
        try {
            Session session = sf.openSession();
            try {
                session.getTransaction().begin();
                session.createNativeQuery("DROP TABLE Simple").executeUpdate();
                session.getTransaction().commit();
            } catch (Throwable t) {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
                throw t;
            } finally {
                session.close();
            }
        } finally {
            sf.close();
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) After(org.junit.After)

Example 5 with BootstrapServiceRegistryBuilder

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

the class SchemaUpdateSchemaNameTest method testSqlAlterWithTableSchemaName.

@Test
public void testSqlAlterWithTableSchemaName() throws Exception {
    StandardServiceRegistry ssr = null;
    try {
        final Configuration cfg = buildConfiguration(SimpleNext.class);
        ssr = new StandardServiceRegistryBuilder(new BootstrapServiceRegistryBuilder().build(), cfg.getStandardServiceRegistryBuilder().getAggregatedCfgXml()).applySettings(cfg.getProperties()).build();
        SessionFactory sf = cfg.buildSessionFactory(ssr);
        try {
            Session session = sf.openSession();
            try {
                session.getTransaction().begin();
                session.createQuery("FROM Simple", SimpleNext.class).getResultList();
                session.getTransaction().commit();
            } catch (Throwable t) {
                if (session.getTransaction().isActive()) {
                    session.getTransaction().rollback();
                }
                throw t;
            } finally {
                session.close();
            }
        } finally {
            sf.close();
        }
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)39 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)34 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)31 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Test (org.junit.Test)21 MetadataSources (org.hibernate.boot.MetadataSources)16 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)12 SeContainer (javax.enterprise.inject.se.SeContainer)8 SeContainerInitializer (javax.enterprise.inject.se.SeContainerInitializer)6 Properties (java.util.Properties)5 Configuration (org.hibernate.cfg.Configuration)5 TheEntity (org.hibernate.test.cdi.events.TheEntity)5 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)4 Integrator (org.hibernate.integrator.spi.Integrator)4 FileInputStream (java.io.FileInputStream)3 SessionFactory (org.hibernate.SessionFactory)3 RegionFactory (org.hibernate.cache.spi.RegionFactory)3 ServiceRegistry (org.hibernate.service.ServiceRegistry)3 Map (java.util.Map)2 Session (org.hibernate.Session)2