Search in sources :

Example 71 with SessionFactory

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

the class ConfigurationTest method testDeclarativeMix.

@Test
public void testDeclarativeMix() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
    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 Plane");
    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)

Example 72 with SessionFactory

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

the class ConfigurationTest method testHbmWithSubclassExtends.

@Test
public void testHbmWithSubclassExtends() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
    cfg.addClass(Ferry.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 Ferry");
    assertEquals(0, q.list().size());
    q = s.createQuery("from Plane");
    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)

Example 73 with SessionFactory

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

the class SessionFactorySerializationTest method testUnNamedSessionFactorySerialization.

@Test
public void testUnNamedSessionFactorySerialization() throws Exception {
    // IMPL NOTE : this test is a control to testNamedSessionFactorySerialization
    // 		here, the test should fail based just on attempted uuid resolution
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactory factory = cfg.buildSessionFactory();
    // we need to do some tricking here so that Hibernate thinks the deserialization happens in a
    // different VM
    String uuid = ((SessionFactoryImplementor) factory).getUuid();
    // deregister under this uuid...
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(uuid, null, false, null);
    // and then register under a different uuid...
    SessionFactoryRegistry.INSTANCE.addSessionFactory("some-other-uuid", null, false, factory, null);
    try {
        SerializationHelper.clone(factory);
        fail("Expecting an error");
    } catch (SerializationException expected) {
    }
    SessionFactoryRegistry.INSTANCE.removeSessionFactory("some-other-uuid", null, false, null);
    factory.close();
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
Also used : SessionFactory(org.hibernate.SessionFactory) SerializationException(org.hibernate.type.SerializationException) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Test(org.junit.Test)

Example 74 with SessionFactory

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

the class SessionFactorySerializationTest method testNamedSessionFactorySerialization.

@Test
public void testNamedSessionFactorySerialization() throws Exception {
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactory factory = cfg.buildSessionFactory();
    // we need to do some tricking here so that Hibernate thinks the deserialization happens in a
    // different VM
    String uuid = ((SessionFactoryImplementor) factory).getUuid();
    // deregister under this uuid...
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(uuid, NAME, false, null);
    // and then register under a different uuid...
    SessionFactoryRegistry.INSTANCE.addSessionFactory("some-other-uuid", NAME, false, factory, null);
    SessionFactory factory2 = (SessionFactory) SerializationHelper.clone(factory);
    assertSame(factory, factory2);
    SessionFactoryRegistry.INSTANCE.removeSessionFactory("some-other-uuid", NAME, false, null);
    factory.close();
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Test(org.junit.Test)

Example 75 with SessionFactory

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

the class PersisterClassProviderTest method testPersisterClassProvider.

@Test
public void testPersisterClassProvider() throws Exception {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Gate.class);
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
    //no exception as the GoofyPersisterClassProvider is not set
    SessionFactory sessionFactory;
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    cfg = new Configuration();
    cfg.addAnnotatedClass(Gate.class);
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The entity persister should be overridden");
    } catch (MappingException e) {
        assertEquals("The entity persister should be overridden", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
    cfg = new Configuration();
    cfg.addAnnotatedClass(Portal.class);
    cfg.addAnnotatedClass(Window.class);
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The collection persister should be overridden but not the entity persister");
    } catch (MappingException e) {
        assertEquals("The collection persister should be overridden but not the entity persister", GoofyPersisterClassProvider.NoopCollectionPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    cfg = new Configuration();
    cfg.addAnnotatedClass(Tree.class);
    cfg.addAnnotatedClass(Palmtree.class);
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).addService(PersisterClassResolver.class, new GoofyPersisterClassProvider()).build();
    try {
        sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        sessionFactory.close();
        fail("The entity persisters should be overridden in a class hierarchy");
    } catch (MappingException e) {
        assertEquals("The entity persisters should be overridden in a class hierarchy", GoofyPersisterClassProvider.NoopEntityPersister.class, ((GoofyException) e.getCause()).getValue());
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
    assertFalse(SessionFactoryRegistry.INSTANCE.hasRegistrations());
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ServiceRegistry(org.hibernate.service.ServiceRegistry) MappingException(org.hibernate.MappingException) 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