Java tutorial
/* * Copyright 2006-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package no.magott.spring; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import java.util.Iterator; import org.hibernate.Query; import org.hibernate.ScrollableResults; import org.hibernate.SessionFactory; import org.hibernate.StatelessSession; import org.hibernate.classic.Session; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class FetchJoinTests { @Autowired private SessionFactory sessionFactory; @Autowired private PlatformTransactionManager transactionManager; @Test public void testScrollVsListOnQuery() throws Exception { TransactionStatus transaction = transactionManager.getTransaction(new DefaultTransactionDefinition()); Session session = sessionFactory.getCurrentSession(); Order order = new Order(); order.getItems().add(createItem("Product 1")); order.getItems().add(createItem("Product 2")); order.getItems().add(createItem("Product 3")); session.save(order); session.flush(); assertNotNull(order.getId()); order = new Order(); order.getItems().add(createItem("Product 4")); order.getItems().add(createItem("Product 5")); order.getItems().add(createItem("Product 6")); session.save(order); session.flush(); assertNotNull(order.getId()); transactionManager.commit(transaction); transaction = transactionManager.getTransaction(new DefaultTransactionDefinition()); StatelessSession stateless = sessionFactory.openStatelessSession(); Query query = stateless.createQuery("select o FROM Order AS o LEFT JOIN FETCH o.items"); int count = query.list().size(); //Strange! assertThat(count, equalTo(6)); count = getCountFromScrollableResults(query); //As expected assertThat(count, equalTo(2)); System.out.println(count); transactionManager.commit(transaction); } private int getCountFromScrollableResults(Query query) { ScrollableResults scrollableResults = query.scroll(); int count; for (count = 0; scrollableResults.next(); count++) { } return count; } private Item createItem(String string) { Item item = new Item(); item.setProduct(string); return item; } }