Search in sources :

Example 36 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class MappingExceptionTest method testInvalidMapping.

@Test
public void testInvalidMapping() throws MappingException, IOException {
    String resourceName = "org/hibernate/test/mappingexception/InvalidMapping.hbm.xml";
    File file = File.createTempFile("TempInvalidMapping", ".hbm.xml");
    file.deleteOnExit();
    copy(ConfigHelper.getConfigStream(resourceName), file);
    Configuration cfg = new Configuration();
    try {
        cfg.addCacheableFile(file.getAbsolutePath());
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "file");
        assertNotNull(inv.getPath());
        assertTrue(inv.getPath().endsWith(".hbm.xml"));
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addCacheableFile(file);
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "file");
        assertNotNull(inv.getPath());
        assertTrue(inv.getPath().endsWith(".hbm.xml"));
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addClass(InvalidMapping.class);
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), "org/hibernate/test/mappingexception/InvalidMapping.hbm.xml");
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addFile(file.getAbsolutePath());
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "file");
        assertEquals(inv.getPath(), file.getPath());
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addFile(file);
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "file");
        assertEquals(inv.getPath(), file.getPath());
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addInputStream(ConfigHelper.getResourceAsStream(resourceName));
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "input stream");
        assertEquals(inv.getPath(), null);
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addResource(resourceName);
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), resourceName);
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addResource(resourceName, getClass().getClassLoader());
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "resource");
        assertEquals(inv.getPath(), resourceName);
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
    try {
        cfg.addURL(ConfigHelper.findAsResource(resourceName));
        fail();
    } catch (InvalidMappingException inv) {
        assertEquals(inv.getType(), "URL");
        assertTrue(inv.getPath().endsWith("InvalidMapping.hbm.xml"));
        assertTrue(!(inv.getCause() instanceof MappingNotFoundException));
    }
}
Also used : MappingNotFoundException(org.hibernate.MappingNotFoundException) Configuration(org.hibernate.cfg.Configuration) InvalidMappingException(org.hibernate.InvalidMappingException) File(java.io.File) Test(org.junit.Test)

Example 37 with Configuration

use of org.hibernate.cfg.Configuration 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 38 with Configuration

use of org.hibernate.cfg.Configuration 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)

Example 39 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class SchemaUpdateSchemaNameTest method buildConfiguration.

private static Configuration buildConfiguration(Class<?> clazz) {
    Configuration cfg = new Configuration();
    cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "update");
    cfg.setProperty(AvailableSettings.SHOW_SQL, "true");
    cfg.setProperty(AvailableSettings.FORMAT_SQL, "true");
    cfg.addAnnotatedClass(clazz);
    return cfg;
}
Also used : Configuration(org.hibernate.cfg.Configuration)

Example 40 with Configuration

use of org.hibernate.cfg.Configuration in project hibernate-orm by hibernate.

the class SchemaUpdateSchemaNameTest method buildInitialSchema.

@Before
public void buildInitialSchema() throws Exception {
    // Builds the initial table in the schema.
    StandardServiceRegistry ssr = null;
    try {
        final Configuration cfg = buildConfiguration(SimpleFirst.class);
        ssr = new StandardServiceRegistryBuilder(new BootstrapServiceRegistryBuilder().build(), cfg.getStandardServiceRegistryBuilder().getAggregatedCfgXml()).applySettings(cfg.getProperties()).build();
        cfg.buildSessionFactory(ssr).close();
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Before(org.junit.Before)

Aggregations

Configuration (org.hibernate.cfg.Configuration)190 Test (org.junit.Test)66 Session (org.hibernate.Session)37 SessionFactory (org.hibernate.SessionFactory)35 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)27 Before (org.junit.Before)20 BeforeClass (org.junit.BeforeClass)20 File (java.io.File)18 ServiceRegistry (org.hibernate.service.ServiceRegistry)14 Properties (java.util.Properties)13 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 HibernateException (org.hibernate.HibernateException)8 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 MappingException (org.hibernate.MappingException)7 Transaction (org.hibernate.Transaction)7 URL (java.net.URL)6 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 TestForIssue (org.hibernate.testing.TestForIssue)6 EntityTuplizer (org.hibernate.tuple.entity.EntityTuplizer)6 BigDecimal (java.math.BigDecimal)5