Search in sources :

Example 36 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class TemplateTest method buildSessionFactory.

@BeforeClass
public static void buildSessionFactory() {
    Configuration cfg = new Configuration();
    cfg.setProperty(AvailableSettings.DIALECT, DIALECT.getClass().getName());
    ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(cfg.getProperties());
    SESSION_FACTORY = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
}
Also used : Configuration(org.hibernate.cfg.Configuration) ServiceRegistry(org.hibernate.service.ServiceRegistry) BeforeClass(org.junit.BeforeClass)

Example 37 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class PersisterClassProviderTest method testPersisterClassProvider.

@Test
public void testPersisterClassProvider() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Gate.class);
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
    //no exception as the GoofyPersisterClassProvider is not set
    SessionFactory sessionFactory;
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    cfg = new Configuration();
    cfg.addAnnotatedClass(Gate.class);
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The entity persister should be overridden");
    } catch (MappingException e) {
        assertEquals("The entity persister should be overridden", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
    cfg = new Configuration();
    cfg.addAnnotatedClass(Portal.class);
    cfg.addAnnotatedClass(Window.class);
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The collection persister should be overridden but not the entity persister");
    } catch (MappingException e) {
        assertEquals("The collection persister should be overridden but not the entity persister", GoofyPersisterClassProvider.NoopCollectionPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    cfg = new Configuration();
    cfg.addAnnotatedClass(Tree.class);
    cfg.addAnnotatedClass(Palmtree.class);
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The entity persisters should be overridden in a class hierarchy");
    } catch (MappingException e) {
        assertEquals("The entity persisters should be overridden in a class hierarchy", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ServiceRegistry(org.hibernate.service.ServiceRegistry) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Example 38 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class BootstrapServiceRegistryImpl method destroy.

@Override
public void destroy() {
    if (!active) {
        return;
    }
    active = false;
    destroy(classLoaderServiceBinding);
    destroy(strategySelectorBinding);
    destroy(integratorServiceBinding);
    if (childRegistries != null) {
        for (ServiceRegistry serviceRegistry : childRegistries) {
            if (serviceRegistry instanceof ServiceRegistryImplementor) {
                ServiceRegistryImplementor serviceRegistryImplementor = (ServiceRegistryImplementor) serviceRegistry;
                serviceRegistryImplementor.destroy();
            }
        }
    }
}
Also used : ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 39 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class OneToOneErrorTest method testWrongOneToOne.

@Test
public void testWrongOneToOne() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Show.class).addAnnotatedClass(ShowDescription.class);
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    ServiceRegistry serviceRegistry = null;
    SessionFactory sessionFactory = null;
    try {
        serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(Environment.getProperties());
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        Assert.fail("Wrong mappedBy does not fail property");
    } catch (AnnotationException e) {
    //success
    } finally {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        if (serviceRegistry != null) {
            ServiceRegistryBuilder.destroy(serviceRegistry);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) AnnotationException(org.hibernate.AnnotationException) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Example 40 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project hibernate-orm by hibernate.

the class SchemaCreatorImpl method doCreation.

public void doCreation(Metadata metadata, final boolean manageNamespaces, GenerationTarget... targets) {
    final ServiceRegistry serviceRegistry = ((MetadataImplementor) metadata).getMetadataBuildingOptions().getServiceRegistry();
    doCreation(metadata, serviceRegistry, serviceRegistry.getService(ConfigurationService.class).getSettings(), manageNamespaces, targets);
}
Also used : ServiceRegistry(org.hibernate.service.ServiceRegistry)

Aggregations

ServiceRegistry (org.hibernate.service.ServiceRegistry)49 Test (org.junit.Test)27 MetadataSources (org.hibernate.boot.MetadataSources)18 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)17 Configuration (org.hibernate.cfg.Configuration)14 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)12 AnnotationException (org.hibernate.AnnotationException)6 MappingException (org.hibernate.MappingException)6 SessionFactory (org.hibernate.SessionFactory)6 Metadata (org.hibernate.boot.Metadata)6 TestForIssue (org.hibernate.testing.TestForIssue)5 Map (java.util.Map)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)4 File (java.io.File)3 HibernateException (org.hibernate.HibernateException)3 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)3 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)3 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2