s.save(i);
t.commit();
s.close();
QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics( queryString );
EntityStatistics es = s.getSessionFactory().getStatistics().getEntityStatistics( Item.class.getName() );
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
List result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 0 );
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 1 );
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
assertEquals( result.size(), 1 );
Map m = (Map) result.get(0);
assertEquals(1, m.size());
t.commit();
s.close();
assertEquals( "hit count should not go up since we are adding a resulttransformer", qs.getCacheHitCount(), 1 );
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
assertEquals( result.size(), 1 );
m = (Map) result.get(0);
assertEquals(1, m.size());
t.commit();
s.close();
assertEquals( "hit count should go up since we are using the same resulttransformer", qs.getCacheHitCount(), 2 );
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
assertTrue( Hibernate.isInitialized( result.get(0) ) );
i = (Item) s.get( Item.class, new Long(i.getId()) );
i.setName("widget");
i.setDescription("A middle-quality widget.");
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 3 );
assertEquals( qs.getCacheMissCount(), 3 );
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
i = (Item) s.get( Item.class, new Long(i.getId()) );
assertEquals( (String) result.get(0), "A middle-quality widget." );
s.delete(i);
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 3 );
assertEquals( qs.getCacheMissCount(), 4 );
assertEquals( qs.getCachePutCount(), 4 );
assertEquals( qs.getExecutionCount(), 4 );
assertEquals( es.getFetchCount(), 0 ); //check that it was being cached
}