Search in sources :

Example 31 with ServiceRegistry

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

the class FetchProfileTest method testXmlOverride.

@Test
public void testXmlOverride() {
    Configuration config = new Configuration();
    config.addAnnotatedClass(Customer5.class);
    config.addAnnotatedClass(Order.class);
    config.addAnnotatedClass(Country.class);
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/hibernate/test/annotations/fetchprofile/mappings.hbm.xml");
    config.addInputStream(is);
    SessionFactoryImplementor sessionImpl = (SessionFactoryImplementor) config.buildSessionFactory(serviceRegistry);
    assertTrue("fetch profile not parsed properly", sessionImpl.containsFetchProfileDefinition("orders-profile"));
    sessionImpl.close();
    // now the same with no xml
    final MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(Customer5.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 : Configuration(org.hibernate.cfg.Configuration) InputStream(java.io.InputStream) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) 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 32 with ServiceRegistry

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

the class FetchProfileTest method testWrongAssociationName.

@Test
public void testWrongAssociationName() {
    final MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(Customer2.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 33 with ServiceRegistry

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

the class DeprecationLoggingTest method basicTest.

@Test
public void basicTest() {
    logInspection.registerListener(LogListenerImpl.INSTANCE);
    MetadataSources metadataSources = new MetadataSources().addResource("org/hibernate/test/entitymode/dom4j/Car.hbm.xml");
    try {
        metadataSources.buildMetadata();
    } 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) Test(org.junit.Test)

Example 34 with ServiceRegistry

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

the class SafeMappingTest method testDeclarativeMix.

@Test
public void testDeclarativeMix() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(IncorrectEntity.class);
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    ServiceRegistry serviceRegistry = null;
    SessionFactory sessionFactory = null;
    try {
        serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(cfg.getProperties());
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        fail("Entity wo id should fail");
    } 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 35 with ServiceRegistry

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

the class SecuredBindingTest method testConfigurationMethods.

@Test
public void testConfigurationMethods() throws Exception {
    Configuration ac = new Configuration();
    Properties p = new Properties();
    p.put(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
    p.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDrive");
    p.put("hibernate.connection.url", "jdbc:hsqldb:.");
    p.put("hibernate.connection.username", "sa");
    p.put("hibernate.connection.password", "");
    p.put("hibernate.show_sql", "true");
    ac.setProperties(p);
    ac.addAnnotatedClass(Plane.class);
    SessionFactory sf = null;
    ServiceRegistry serviceRegistry = null;
    try {
        serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(p);
        sf = ac.buildSessionFactory(serviceRegistry);
        try {
            sf.close();
        } catch (Exception ignore) {
        }
        Assert.fail("Driver property overriding should work");
    } catch (HibernateException he) {
    //success
    } finally {
        if (sf != null) {
            sf.close();
        }
        if (serviceRegistry != null) {
            ServiceRegistryBuilder.destroy(serviceRegistry);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) ServiceRegistry(org.hibernate.service.ServiceRegistry) Properties(java.util.Properties) HibernateException(org.hibernate.HibernateException) Test(org.junit.Test)

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