Search in sources :

Example 66 with SessionFactory

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

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

Example 68 with SessionFactory

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

the class AccessMappingTest method testInconsistentAnnotationPlacement.

@Test
public void testInconsistentAnnotationPlacement() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Course1.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 69 with SessionFactory

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

the class ConfigurationTest method testPrecedenceAnnotation.

@Test
public void testPrecedenceAnnotation() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    cfg.setProperty(Configuration.ARTEFACT_PROCESSING_ORDER, "class, hbm");
    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);
    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 70 with SessionFactory

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

the class ConfigurationTest method testAnnReferencesHbm.

@Test
public void testAnnReferencesHbm() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
    cfg.addAnnotatedClass(Port.class);
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    SessionFactory sf = cfg.buildSessionFactory();
    assertNotNull(sf);
    Session s = sf.openSession();
    Transaction tx = s.beginTransaction();
    Query q = s.createQuery("from Boat");
    assertEquals(0, q.list().size());
    q = s.createQuery("from Port");
    assertEquals(0, q.list().size());
    tx.commit();
    s.close();
    sf.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SessionFactory (org.hibernate.SessionFactory)108 Test (org.junit.Test)62 Session (org.hibernate.Session)50 Configuration (org.hibernate.cfg.Configuration)35 Transaction (org.hibernate.Transaction)20 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