Search in sources :

Example 16 with ServiceRegistry

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

the class DuplicateTest method testDuplicateEntityName.

@Test
public void testDuplicateEntityName() throws Exception {
    Configuration cfg = new Configuration();
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    ServiceRegistry serviceRegistry = null;
    SessionFactory sf = null;
    try {
        cfg.addAnnotatedClass(Flight.class);
        cfg.addAnnotatedClass(org.hibernate.test.annotations.Flight.class);
        cfg.addResource("org/hibernate/test/annotations/orm.xml");
        cfg.addResource("org/hibernate/test/annotations/duplicatedgenerator/orm.xml");
        serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(cfg.getProperties());
        sf = cfg.buildSessionFactory(serviceRegistry);
        Assert.fail("Should not be able to map the same entity name twice");
    } catch (AnnotationException ae) {
    //success
    } finally {
        if (sf != null) {
            sf.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 17 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry 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 18 with ServiceRegistry

use of org.hibernate.service.ServiceRegistry 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)

Example 19 with ServiceRegistry

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

the class NullablePrimaryKeyTest method testGeneratedSql.

@Test
@SuppressWarnings("unchecked")
public void testGeneratedSql() {
    Map settings = new HashMap();
    settings.putAll(Environment.getProperties());
    settings.put(AvailableSettings.DIALECT, SQLServerDialect.class.getName());
    ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(settings);
    try {
        MetadataSources ms = new MetadataSources(serviceRegistry);
        ms.addAnnotatedClass(Address.class);
        ms.addAnnotatedClass(Person.class);
        final Metadata metadata = ms.buildMetadata();
        final List<String> commands = new SchemaCreatorImpl(serviceRegistry).generateCreationCommands(metadata, false);
        for (String s : commands) {
            log.debug(s);
        }
        String expectedMappingTableSql = "create table personAddress (address_id numeric(19,0), " + "person_id numeric(19,0) not null, primary key (person_id))";
        Assert.assertEquals("Wrong SQL", expectedMappingTableSql, commands.get(2));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : SQLServerDialect(org.hibernate.dialect.SQLServerDialect) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) HashMap(java.util.HashMap) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) ServiceRegistry(org.hibernate.service.ServiceRegistry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 20 with ServiceRegistry

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

the class InvalidEnumeratedJavaTypeTest method testInvalidMapping.

@Test
public void testInvalidMapping() {
    MetadataSources metadataSources = new MetadataSources().addAnnotatedClass(TheEntity.class);
    try {
        metadataSources.buildMetadata();
        fail("Was expecting failure");
    } catch (AnnotationException ignore) {
    } finally {
        ServiceRegistry metaServiceRegistry = metadataSources.getServiceRegistry();
        if (metaServiceRegistry instanceof BootstrapServiceRegistry) {
            BootstrapServiceRegistryBuilder.destroy(metaServiceRegistry);
        }
    }
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) AnnotationException(org.hibernate.AnnotationException) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) ServiceRegistry(org.hibernate.service.ServiceRegistry) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) Test(org.junit.Test)

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