Search in sources :

Example 21 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class ConfigurationTest method testPrecedenceHbm.

@Test
public void testPrecedenceHbm() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    cfg.addAnnotatedClass(Boat.class);
    SessionFactory sf = cfg.buildSessionFactory();
    assertNotNull(sf);
    Session s = sf.openSession();
    s.getTransaction().begin();
    Boat boat = new Boat();
    boat.setSize(12);
    boat.setWeight(34);
    s.persist(boat);
    s.getTransaction().commit();
    s.clear();
    Transaction tx = s.beginTransaction();
    boat = (Boat) s.get(Boat.class, boat.getId());
    assertTrue("Annotation has precedence", 34 != boat.getWeight());
    s.delete(boat);
    //s.getTransaction().commit();
    tx.commit();
    s.close();
    sf.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class ServiceRegistryClosingCascadeTest method testSessionFactoryClosing.

@Test
public void testSessionFactoryClosing() {
    BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
    StandardServiceRegistry sr = new StandardServiceRegistryBuilder(bsr).build();
    assertTrue(((BootstrapServiceRegistryImpl) bsr).isActive());
    Configuration config = new Configuration();
    SessionFactory sf = config.buildSessionFactory(sr);
    sf.close();
    assertFalse(((BootstrapServiceRegistryImpl) bsr).isActive());
}
Also used : SessionFactory(org.hibernate.SessionFactory) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Configuration(org.hibernate.cfg.Configuration) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 23 with SessionFactory

use of org.hibernate.SessionFactory 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 24 with SessionFactory

use of org.hibernate.SessionFactory 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 25 with SessionFactory

use of org.hibernate.SessionFactory 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

SessionFactory (org.hibernate.SessionFactory)106 Test (org.junit.Test)62 Session (org.hibernate.Session)49 Configuration (org.hibernate.cfg.Configuration)34 Transaction (org.hibernate.Transaction)19 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)19 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)13 MetadataSources (org.hibernate.boot.MetadataSources)11 HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Properties (java.util.Properties)8 Query (org.hibernate.Query)8 Metadata (org.hibernate.boot.Metadata)8 ArrayList (java.util.ArrayList)7 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 TimeUnit (java.util.concurrent.TimeUnit)5 AnnotationException (org.hibernate.AnnotationException)5 Collections (java.util.Collections)4