Search in sources :

Example 16 with QueryStatistics

use of org.hibernate.stat.QueryStatistics in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testDynamicInstantiationQueries.

@Test
public void testDynamicInstantiationQueries() throws Exception {
    createTestBaseData();
    Session session = openSession();
    Transaction t = session.beginTransaction();
    List results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertClassAssignability(results.get(0).getClass(), Animal.class);
    Iterator iter = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    assertTrue("Incorrect return type", iter.next() instanceof Animal);
    results = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof List);
    assertEquals("Incorrect return type", ((List) results.get(0)).size(), 2);
    results = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof List);
    assertEquals("Incorrect return type", ((List) results.get(0)).size(), 2);
    iter = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    Object obj = iter.next();
    assertTrue("Incorrect return type", obj instanceof List);
    assertEquals("Incorrect return type", ((List) obj).size(), 2);
    iter = session.createQuery("select new list(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    obj = iter.next();
    assertTrue("Incorrect return type", obj instanceof List);
    assertEquals("Incorrect return type", ((List) obj).size(), 2);
    results = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof Map);
    assertEquals("Incorrect return type", ((Map) results.get(0)).size(), 2);
    assertTrue(((Map) results.get(0)).containsKey("0"));
    assertTrue(((Map) results.get(0)).containsKey("1"));
    results = session.createQuery("select new map(an.description as descr, an.bodyWeight as bw) from Animal an").list();
    assertEquals("Incorrect result size", 2, results.size());
    assertTrue("Incorrect return type", results.get(0) instanceof Map);
    assertEquals("Incorrect return type", ((Map) results.get(0)).size(), 2);
    assertTrue(((Map) results.get(0)).containsKey("descr"));
    assertTrue(((Map) results.get(0)).containsKey("bw"));
    iter = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").iterate();
    assertTrue("Incorrect result size", iter.hasNext());
    obj = iter.next();
    assertTrue("Incorrect return type", obj instanceof Map);
    assertEquals("Incorrect return type", ((Map) obj).size(), 2);
    ScrollableResults sr = session.createQuery("select new map(an.description, an.bodyWeight) from Animal an").scroll();
    assertTrue("Incorrect result size", sr.next());
    obj = sr.get(0);
    assertTrue("Incorrect return type", obj instanceof Map);
    assertEquals("Incorrect return type", ((Map) obj).size(), 2);
    sr.close();
    sr = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").scroll();
    assertTrue("Incorrect result size", sr.next());
    assertTrue("Incorrect return type", sr.get(0) instanceof Animal);
    sr.close();
    // caching...
    QueryStatistics stats = sessionFactory().getStatistics().getQueryStatistics("select new Animal(an.description, an.bodyWeight) from Animal an");
    results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").setCacheable(true).list();
    assertEquals("incorrect result size", 2, results.size());
    assertClassAssignability(Animal.class, results.get(0).getClass());
    long initCacheHits = stats.getCacheHitCount();
    results = session.createQuery("select new Animal(an.description, an.bodyWeight) from Animal an").setCacheable(true).list();
    assertEquals("dynamic intantiation query not served from cache", initCacheHits + 1, stats.getCacheHitCount());
    assertEquals("incorrect result size", 2, results.size());
    assertClassAssignability(Animal.class, results.get(0).getClass());
    t.commit();
    session.close();
    destroyTestBaseData();
}
Also used : Transaction(org.hibernate.Transaction) TransactionUtil2.inTransaction(org.hibernate.testing.transaction.TransactionUtil2.inTransaction) QueryStatistics(org.hibernate.stat.QueryStatistics) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ScrollableResults(org.hibernate.ScrollableResults) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

QueryStatistics (org.hibernate.stat.QueryStatistics)16 Statistics (org.hibernate.stat.Statistics)9 List (java.util.List)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 Session (org.hibernate.Session)6 Transaction (org.hibernate.Transaction)5 EntityManager (javax.persistence.EntityManager)4 Query (javax.persistence.Query)4 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)3 Date (java.util.Date)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ScrollableResults (org.hibernate.ScrollableResults)2 SessionFactory (org.hibernate.SessionFactory)2 EntityStatistics (org.hibernate.stat.EntityStatistics)2 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)2 HashMap (java.util.HashMap)1 UserTransaction (javax.transaction.UserTransaction)1 StatsEntry (org.bedework.calfacade.BwStats.StatsEntry)1