Search in sources :

Example 56 with SessionFactory

use of org.hibernate.SessionFactory in project ignite by apache.

the class HibernateL2CacheConfigurationSelfTest method testCacheUsage.

/**
 * @param expCache1 Expected size of cache with name 'cache1'.
 * @param expCache2 Expected size of cache with name 'cache2'.
 * @param expCache3 Expected size of cache with name 'cache3'.
 * @param expCacheE3 Expected size of cache with name {@link #ENTITY3_NAME}.
 * @param expCacheE4 Expected size of cache with name {@link #ENTITY4_NAME}.
 */
@SuppressWarnings("unchecked")
private void testCacheUsage(int expCache1, int expCache2, int expCache3, int expCacheE3, int expCacheE4) {
    SessionFactory sesFactory = startHibernate(getTestIgniteInstanceName(0));
    try {
        Session ses = sesFactory.openSession();
        try {
            Transaction tx = ses.beginTransaction();
            ses.save(new Entity1());
            ses.save(new Entity2());
            ses.save(new Entity3());
            ses.save(new Entity4());
            tx.commit();
        } finally {
            ses.close();
        }
        ses = sesFactory.openSession();
        try {
            List<Entity1> list1 = ses.createCriteria(ENTITY1_NAME).list();
            assertEquals(1, list1.size());
            for (Entity1 e : list1) {
                ses.load(ENTITY1_NAME, e.getId());
                assertNotNull(e.getId());
            }
            List<Entity2> list2 = ses.createCriteria(ENTITY2_NAME).list();
            assertEquals(1, list2.size());
            for (Entity2 e : list2) assertNotNull(e.getId());
            List<Entity3> list3 = ses.createCriteria(ENTITY3_NAME).list();
            assertEquals(1, list3.size());
            for (Entity3 e : list3) assertNotNull(e.getId());
            List<Entity4> list4 = ses.createCriteria(ENTITY4_NAME).list();
            assertEquals(1, list4.size());
            for (Entity4 e : list4) assertNotNull(e.getId());
        } finally {
            ses.close();
        }
        IgniteCache<Object, Object> cache1 = grid(0).cache("cache1");
        IgniteCache<Object, Object> cache2 = grid(0).cache("cache2");
        IgniteCache<Object, Object> cache3 = grid(0).cache("cache3");
        IgniteCache<Object, Object> cacheE3 = grid(0).cache(ENTITY3_NAME);
        IgniteCache<Object, Object> cacheE4 = grid(0).cache(ENTITY4_NAME);
        assertEquals("Unexpected entries: " + toSet(cache1.iterator()), expCache1, cache1.size());
        assertEquals("Unexpected entries: " + toSet(cache2.iterator()), expCache2, cache2.size());
        assertEquals("Unexpected entries: " + toSet(cache3.iterator()), expCache3, cache3.size());
        assertEquals("Unexpected entries: " + toSet(cacheE3.iterator()), expCacheE3, cacheE3.size());
        assertEquals("Unexpected entries: " + toSet(cacheE4.iterator()), expCacheE4, cacheE4.size());
    } finally {
        sesFactory.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 57 with SessionFactory

use of org.hibernate.SessionFactory in project scheduling by ow2-proactive.

the class TransactionHelperTest method setUp.

@Before
public void setUp() {
    sessionFactory = mock(SessionFactory.class);
    session = mock(Session.class);
    transaction = mock(Transaction.class);
    when(sessionFactory.openSession()).thenReturn(session);
    when(session.beginTransaction()).thenReturn(transaction);
    when(session.getTransaction()).thenReturn(transaction);
    transactionHelper = new TransactionHelper(sessionFactory);
    sessionWork = mock(SessionWork.class);
}
Also used : SessionFactory(org.hibernate.SessionFactory) SessionWork(org.ow2.proactive.db.SessionWork) Transaction(org.hibernate.Transaction) TransactionHelper(org.ow2.proactive.db.TransactionHelper) Session(org.hibernate.Session) Before(org.junit.Before)

Example 58 with SessionFactory

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

the class OsgiIntegrationTest method testNativeEnvers.

@Test
public void testNativeEnvers() throws Exception {
    final ServiceReference sr = bundleContext.getServiceReference(SessionFactory.class.getName());
    final SessionFactory sf = (SessionFactory) bundleContext.getService(sr);
    final Integer adpId;
    Session s = sf.openSession();
    s.getTransaction().begin();
    AuditedDataPoint adp = new AuditedDataPoint("Chris");
    s.persist(adp);
    s.getTransaction().commit();
    adpId = adp.getId();
    s.close();
    s = sf.openSession();
    s.getTransaction().begin();
    adp = s.get(AuditedDataPoint.class, adpId);
    adp.setName("Chris2");
    s.getTransaction().commit();
    s.close();
    s = sf.openSession();
    AuditReader ar = AuditReaderFactory.get(s);
    assertEquals(2, ar.getRevisions(AuditedDataPoint.class, adpId).size());
    AuditedDataPoint rev1 = ar.find(AuditedDataPoint.class, adpId, 1);
    AuditedDataPoint rev2 = ar.find(AuditedDataPoint.class, adpId, 2);
    assertEquals(new AuditedDataPoint(adpId, "Chris"), rev1);
    assertEquals(new AuditedDataPoint(adpId, "Chris2"), rev2);
    s.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) AuditedDataPoint(org.hibernate.osgi.test.client.AuditedDataPoint) AuditReader(org.hibernate.envers.AuditReader) ServiceReference(org.osgi.framework.ServiceReference) Session(org.hibernate.Session) Test(org.junit.Test)

Example 59 with SessionFactory

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

the class OsgiIntegrationTest method testNative.

@Test
public void testNative() throws Exception {
    final ServiceReference sr = bundleContext.getServiceReference(SessionFactory.class.getName());
    final SessionFactory sf = (SessionFactory) bundleContext.getService(sr);
    Session s = sf.openSession();
    s.getTransaction().begin();
    s.persist(new DataPoint("Brett"));
    s.getTransaction().commit();
    s.close();
    s = sf.openSession();
    s.getTransaction().begin();
    DataPoint dp = (DataPoint) s.get(DataPoint.class, 1);
    assertNotNull(dp);
    assertEquals("Brett", dp.getName());
    s.getTransaction().commit();
    s.close();
    dp.setName("Brett2");
    s = sf.openSession();
    s.getTransaction().begin();
    s.update(dp);
    s.getTransaction().commit();
    s.close();
    s = sf.openSession();
    s.getTransaction().begin();
    dp = (DataPoint) s.get(DataPoint.class, 1);
    assertNotNull(dp);
    assertEquals("Brett2", dp.getName());
    s.getTransaction().commit();
    s.close();
    s = sf.openSession();
    s.getTransaction().begin();
    s.createQuery("delete from DataPoint").executeUpdate();
    s.getTransaction().commit();
    s.close();
    s = sf.openSession();
    s.getTransaction().begin();
    dp = (DataPoint) s.get(DataPoint.class, 1);
    assertNull(dp);
    s.getTransaction().commit();
    s.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) DataPoint(org.hibernate.osgi.test.client.DataPoint) AuditedDataPoint(org.hibernate.osgi.test.client.AuditedDataPoint) ServiceReference(org.osgi.framework.ServiceReference) Session(org.hibernate.Session) Test(org.junit.Test)

Example 60 with SessionFactory

use of org.hibernate.SessionFactory in project dropwizard by dropwizard.

the class SessionFactoryFactory method build.

public SessionFactory build(HibernateBundle<?> bundle, Environment environment, PooledDataSourceFactory dbConfig, ManagedDataSource dataSource, List<Class<?>> entities) {
    final ConnectionProvider provider = buildConnectionProvider(dataSource, dbConfig.getProperties());
    final SessionFactory factory = buildSessionFactory(bundle, dbConfig, provider, dbConfig.getProperties(), entities);
    final SessionFactoryManager managedFactory = new SessionFactoryManager(factory, dataSource);
    environment.lifecycle().manage(managedFactory);
    return factory;
}
Also used : SessionFactory(org.hibernate.SessionFactory) ConnectionProvider(org.hibernate.engine.jdbc.connections.spi.ConnectionProvider)

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