/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2006-2011, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.test.iterate; import java.util.Iterator; import org.hibernate.Hibernate; import org.hibernate.ScrollableResults; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.junit.Test; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** * @author Gavin King */ public class IterateTest extends BaseCoreFunctionalTestCase { public String[] getMappings() { return new String[] { "iterate/Item.hbm.xml" }; } public void configure(Configuration cfg) { super.configure( cfg ); cfg.setProperty( Environment.USE_QUERY_CACHE, "true" ); cfg.setProperty( Environment.CACHE_REGION_PREFIX, "foo" ); cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" ); cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); } @Test public void testIterate() throws Exception { sessionFactory().getStatistics().clear(); Session s = openSession(); Transaction t = s.beginTransaction(); Item i1 = new Item("foo"); Item i2 = new Item("bar"); s.persist("Item", i1); s.persist("Item", i2); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Iterator iter = s.getNamedQuery("Item.nameDesc").iterate(); i1 = (Item) iter.next(); i2 = (Item) iter.next(); assertFalse( Hibernate.isInitialized(i1) ); assertFalse( Hibernate.isInitialized(i2) ); i1.getName(); i2.getName(); assertFalse( Hibernate.isInitialized(i1) ); assertFalse( Hibernate.isInitialized(i2) ); assertEquals( i1.getName(), "foo" ); assertEquals( i2.getName(), "bar" ); Hibernate.initialize(i1); assertFalse( iter.hasNext() ); s.delete(i1); s.delete(i2); t.commit(); s.close(); assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 2 ); } @Test public void testScroll() throws Exception { sessionFactory().getStatistics().clear(); Session s = openSession(); Transaction t = s.beginTransaction(); Item i1 = new Item("foo"); Item i2 = new Item("bar"); s.persist("Item", i1); s.persist("Item", i2); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); ScrollableResults sr = s.getNamedQuery("Item.nameDesc").scroll(); assertTrue( sr.next() ); i1 = (Item) sr.get(0); assertTrue( sr.next() ); i2 = (Item) sr.get(0); assertTrue( Hibernate.isInitialized(i1) ); assertTrue( Hibernate.isInitialized(i2) ); assertEquals( i1.getName(), "foo" ); assertEquals( i2.getName(), "bar" ); assertFalse( sr.next() ); s.delete(i1); s.delete(i2); t.commit(); s.close(); assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); } }