Search in sources :

Example 16 with Configuration

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

the class AccessMappingTest method testDefaultPropertyAccessIsInherited.

@Test
public void testDefaultPropertyAccessIsInherited() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Horse.class);
    cfg.addAnnotatedClass(Animal.class);
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory(serviceRegistry);
    try {
        EntityTuplizer tuplizer = factory.getEntityPersister(Animal.class.getName()).getEntityMetamodel().getTuplizer();
        assertTrue("Property access should be used since explicity configured via @Access", tuplizer.getIdentifierGetter() instanceof GetterMethodImpl);
        tuplizer = factory.getEntityPersister(Horse.class.getName()).getEntityMetamodel().getTuplizer();
        assertTrue("Field access should be used since the default access mode gets inherited", tuplizer.getGetter(0) instanceof GetterFieldImpl);
    } finally {
        factory.close();
    }
}
Also used : EntityTuplizer(org.hibernate.tuple.entity.EntityTuplizer) GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) Test(org.junit.Test)

Example 17 with Configuration

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

the class AccessMappingTest method testExplicitPropertyAccessAnnotationsOnField.

@Test
public void testExplicitPropertyAccessAnnotationsOnField() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Course4.class);
    cfg.addAnnotatedClass(Student.class);
    SessionFactory sf = null;
    try {
        sf = cfg.buildSessionFactory(serviceRegistry);
        fail("@Id and @OneToMany are not placed consistently in test entities. SessionFactory creation should fail.");
    } catch (MappingException e) {
    // success
    } finally {
        if (sf != null) {
            sf.close();
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) MappingException(org.hibernate.MappingException) Test(org.junit.Test)

Example 18 with Configuration

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

the class SubclassProxyInterfaceTest method testSubclassProxyInterfaces.

@Test
public void testSubclassProxyInterfaces() {
    final Configuration cfg = new Configuration().setProperty(Environment.DIALECT, H2Dialect.class.getName()).addClass(Person.class);
    ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(cfg.getProperties());
    cfg.buildSessionFactory(serviceRegistry).close();
    ServiceRegistryBuilder.destroy(serviceRegistry);
}
Also used : Configuration(org.hibernate.cfg.Configuration) ServiceRegistry(org.hibernate.service.ServiceRegistry) Test(org.junit.Test)

Example 19 with Configuration

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

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

the class EmbeddableIntegratorTest method testWithTypeContributor.

@Test
public void testWithTypeContributor() {
    SessionFactory sf = new Configuration().addAnnotatedClass(Investor.class).registerTypeContributor(new InvestorTypeContributor()).setProperty("hibernate.hbm2ddl.auto", "create-drop").buildSessionFactory();
    Session sess = sf.openSession();
    try {
        sess.getTransaction().begin();
        Investor myInv = getInvestor();
        myInv.setId(2L);
        sess.save(myInv);
        sess.flush();
        sess.clear();
        Investor inv = (Investor) sess.get(Investor.class, 2L);
        assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
    } catch (Exception e) {
        sess.getTransaction().rollback();
        throw e;
    } finally {
        sess.close();
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) BigDecimal(java.math.BigDecimal) PersistenceException(javax.persistence.PersistenceException) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Configuration (org.hibernate.cfg.Configuration)179 Test (org.junit.Test)66 Session (org.hibernate.Session)37 SessionFactory (org.hibernate.SessionFactory)34 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)27 BeforeClass (org.junit.BeforeClass)19 Before (org.junit.Before)18 ServiceRegistry (org.hibernate.service.ServiceRegistry)14 File (java.io.File)13 Properties (java.util.Properties)11 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 HibernateException (org.hibernate.HibernateException)7 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