Search in sources :

Example 46 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 47 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 48 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry in project wildfly by wildfly.

the class SFSBHibernateSFNaturalId method setupConfig.

public void setupConfig() {
    // static {
    try {
        // prepare the configuration
        Configuration configuration = new Configuration().setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
        configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
        configuration.setProperty(Environment.DATASOURCE, "java:jboss/datasources/ExampleDS");
        // configuration.configure("hibernate.cfg.xml");
        // fetch the properties
        Properties properties = new Properties();
        properties.putAll(configuration.getProperties());
        Environment.verifyProperties(properties);
        ConfigurationHelper.resolvePlaceHolders(properties);
        // build the serviceregistry
        final BootstrapServiceRegistryBuilder bootstrapbuilder = new BootstrapServiceRegistryBuilder();
        ServiceRegistry serviceRegistry = builder.build();
        // Create the SessionFactory from Configuration
        sessionFactory = configuration.configure("hibernate.cfg.xml").buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        throw new RuntimeException("Could not setup config", ex);
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) ServiceRegistry(org.hibernate.service.ServiceRegistry) Properties(java.util.Properties)

Aggregations

ServiceRegistry (org.hibernate.service.ServiceRegistry)48 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)11 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 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 File (java.io.File)2