public static Test suite() {
return new FunctionalTestClassTestSuite( StatsTest.class );
}
public void testCollectionFetchVsLoad() throws Exception {
Statistics stats = getSessions().getStatistics();
stats.clear();
Session s = openSession();
Transaction tx = s.beginTransaction();
Continent europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
assertEquals(0, stats.getCollectionLoadCount() );
assertEquals(0, stats.getCollectionFetchCount() );
Continent europe2 = (Continent) s.get( Continent.class, europe.getId() );
assertEquals("Lazy true: no collection should be loaded", 0, stats.getCollectionLoadCount() );
assertEquals( 0, stats.getCollectionFetchCount() );
europe2.getCountries().size();
assertEquals( 1, stats.getCollectionLoadCount() );
assertEquals("Explicit fetch of the collection state", 1, stats.getCollectionFetchCount() );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
stats.clear();
europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
assertEquals( 0, stats.getCollectionLoadCount() );
assertEquals( 0, stats.getCollectionFetchCount() );
europe2 = (Continent) s.createQuery(
"from " + Continent.class.getName() + " a join fetch a.countries where a.id = " + europe.getId()
).uniqueResult();
assertEquals( 1, stats.getCollectionLoadCount() );
assertEquals( "collection should be loaded in the same query as its parent", 0, stats.getCollectionFetchCount() );
tx.commit();
s.close();
Collection coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
coll.setFetchMode(FetchMode.JOIN);
coll.setLazy(false);
SessionFactory sf = getCfg().buildSessionFactory();
stats = sf.getStatistics();
stats.clear();
stats.setStatisticsEnabled(true);
s = sf.openSession();
tx = s.beginTransaction();
europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
assertEquals( 0, stats.getCollectionLoadCount() );
assertEquals( 0, stats.getCollectionFetchCount() );
europe2 = (Continent) s.get( Continent.class, europe.getId() );
assertEquals( 1, stats.getCollectionLoadCount() );
assertEquals( "Should do direct load, not indirect second load when lazy false and JOIN", 0, stats.getCollectionFetchCount() );
tx.commit();
s.close();
sf.close();
coll = getCfg().getCollectionMapping(Continent.class.getName() + ".countries");
coll.setFetchMode(FetchMode.SELECT);
coll.setLazy(false);
sf = getCfg().buildSessionFactory();
stats = sf.getStatistics();
stats.clear();
stats.setStatisticsEnabled(true);
s = sf.openSession();
tx = s.beginTransaction();
europe = fillDb(s);
tx.commit();
s.clear();
tx = s.beginTransaction();
assertEquals( 0, stats.getCollectionLoadCount() );
assertEquals( 0, stats.getCollectionFetchCount() );
europe2 = (Continent) s.get( Continent.class, europe.getId() );
assertEquals( 1, stats.getCollectionLoadCount() );
assertEquals( "Should do explicit collection load, not part of the first one", 1, stats.getCollectionFetchCount() );
Iterator countries = europe2.getCountries().iterator();
while ( countries.hasNext() ) {
s.delete( countries.next() );
}
cleanDb( s );