Search in sources :

Example 36 with SessionFactory

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

the class AccountDAO method getTotalBalance.

public int getTotalBalance(AccountHolder holder, boolean useRegion) throws Exception {
    List results = (List) withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session.createQuery("select account.balance from Account as account where account.accountHolder = ?");
        query.setParameter(0, holder);
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return query.list();
    });
    int total = 0;
    if (results != null) {
        for (Iterator it = results.iterator(); it.hasNext(); ) {
            total += ((Integer) it.next()).intValue();
            System.out.println("Total = " + total);
        }
    }
    return total;
}
Also used : AccountHolder(org.hibernate.test.cache.infinispan.functional.entities.AccountHolder) List(java.util.List) Iterator(java.util.Iterator) TxUtil.withTxSession(org.hibernate.test.cache.infinispan.util.TxUtil.withTxSession) SessionFactory(org.hibernate.SessionFactory) Query(org.hibernate.Query) Account(org.hibernate.test.cache.infinispan.functional.entities.Account) InfinispanMessageLogger(org.hibernate.cache.infinispan.util.InfinispanMessageLogger) TxUtil.withTxSessionApply(org.hibernate.test.cache.infinispan.util.TxUtil.withTxSessionApply) Query(org.hibernate.Query) Iterator(java.util.Iterator) List(java.util.List)

Example 37 with SessionFactory

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

the class CacheKeysFactoryTest method test.

private void test(String cacheKeysFactory, String keyClassName) throws Exception {
    SessionFactory sessionFactory = getSessionFactory(cacheKeysFactory);
    withTxSession(false, sessionFactory, s -> {
        Person person = new Person("John", "Black", 39);
        s.persist(person);
    });
    TestInfinispanRegionFactory regionFactory = (TestInfinispanRegionFactory) ((CacheImplementor) sessionFactory.getCache()).getRegionFactory();
    Cache<Object, Object> cache = regionFactory.getCacheManager().getCache(Person.class.getName());
    Iterator<Object> iterator = cache.getAdvancedCache().getDataContainer().keySet().iterator();
    assertTrue(iterator.hasNext());
    Object key = iterator.next();
    assertEquals(keyClassName, key.getClass().getSimpleName());
    withTxSession(false, sessionFactory, s -> {
        Person person = s.load(Person.class, new Name("John", "Black"));
        assertEquals(39, person.getAge());
    });
}
Also used : SessionFactory(org.hibernate.SessionFactory) Person(org.hibernate.test.cache.infinispan.functional.entities.Person) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Name(org.hibernate.test.cache.infinispan.functional.entities.Name)

Example 38 with SessionFactory

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

the class DAOTestRuleTest method ruleCreatedSessionFactory.

@Test
public void ruleCreatedSessionFactory() {
    final SessionFactory sessionFactory = daoTestRule.getSessionFactory();
    assertThat(sessionFactory).isNotNull();
}
Also used : SessionFactory(org.hibernate.SessionFactory) Test(org.junit.Test)

Example 39 with SessionFactory

use of org.hibernate.SessionFactory in project yyl_example by Relucent.

the class HibernateTest method main.

public static void main(String[] args) {
    Configuration configuration = new Configuration().configure("/yyl/example/demo/hibernate/config/hibernate.cfg.xml");
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    SessionFactory sessionFactory = null;
    Session session = null;
    try {
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        test1(sessionFactory);
        test2(sessionFactory);
        test3(sessionFactory);
    } finally {
        closeQuietly(session);
        closeQuietly(sessionFactory);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ServiceRegistry(org.hibernate.service.ServiceRegistry) Session(org.hibernate.Session)

Example 40 with SessionFactory

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

the class HibernateCollectionStatistics method getBaseStatistics.

private org.hibernate.stat.Statistics getBaseStatistics(EntityManagerFactory entityManagerFactory) {
    if (entityManagerFactory == null) {
        return null;
    }
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics();
    }
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory)

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