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();
}
Aggregations