1 package org.hibernate.test.batchfetch; 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import junit.framework.Test; 9 import junit.framework.TestSuite; 10 11 import org.hibernate.Hibernate; 12 import org.hibernate.Session; 13 import org.hibernate.Transaction; 14 import org.hibernate.test.TestCase; 15 16 19 public class BatchFetchTest extends TestCase { 20 21 public BatchFetchTest(String str) { 22 super(str); 23 } 24 25 public void testBatchFetch() { 26 Session s = openSession(); 27 Transaction t = s.beginTransaction(); 28 ProductLine cars = new ProductLine(); 29 cars.setDescription("Cars"); 30 Model monaro = new Model(cars); 31 monaro.setName("monaro"); 32 monaro.setDescription("Holden Monaro"); 33 Model hsv = new Model(cars); 34 hsv.setName("hsv"); 35 hsv.setDescription("Holden Commodore HSV"); 36 s.save(cars); 37 38 ProductLine oss = new ProductLine(); 39 oss.setDescription("OSS"); 40 Model jboss = new Model(oss); 41 jboss.setName("JBoss"); 42 jboss.setDescription("JBoss Application Server"); 43 Model hibernate = new Model(oss); 44 hibernate.setName("Hibernate"); 45 hibernate.setDescription("Hibernate"); 46 Model cache = new Model(oss); 47 cache.setName("JBossCache"); 48 cache.setDescription("JBoss TreeCache"); 49 s.save(oss); 50 51 t.commit(); 52 s.close(); 53 54 s.getSessionFactory().evict(Model.class); 55 s.getSessionFactory().evict(ProductLine.class); 56 57 s = openSession(); 58 t = s.beginTransaction(); 59 60 List list = s.createQuery("from ProductLine pl order by pl.description").list(); 61 cars = (ProductLine) list.get(0); 62 oss = (ProductLine) list.get(1); 63 assertFalse( Hibernate.isInitialized( cars.getModels() ) ); 64 assertFalse( Hibernate.isInitialized( oss.getModels() ) ); 65 assertEquals( cars.getModels().size(), 2 ); assertTrue( Hibernate.isInitialized( cars.getModels() ) ); 67 assertTrue( Hibernate.isInitialized( oss.getModels() ) ); 68 69 s.clear(); 70 71 list = s.createQuery("from Model m").list(); 72 hibernate = (Model) s.get(Model.class, hibernate.getId()); 73 hibernate.getProductLine().getId(); 74 for ( Iterator i=list.iterator(); i.hasNext(); ) { 75 assertFalse( Hibernate.isInitialized( ( (Model) i.next() ).getProductLine() ) ); 76 } 77 assertEquals( hibernate.getProductLine().getDescription(), "OSS" ); 79 s.clear(); 80 81 Iterator iter = s.createQuery("from Model").iterate(); 82 list = new ArrayList (); 83 while ( iter.hasNext() ) { 84 list.add( iter.next() ); 85 } 86 Model m = (Model) list.get(0); 87 m.getDescription(); 89 s.clear(); 90 91 list = s.createQuery("from ProductLine").list(); 92 ProductLine pl = (ProductLine) list.get(0); 93 ProductLine pl2 = (ProductLine) list.get(1); 94 s.evict(pl2); 95 pl.getModels().size(); 97 t.commit(); 98 s.close(); 99 100 s = openSession(); 101 t = s.beginTransaction(); 102 list = s.createQuery("from ProductLine pl order by pl.description").list(); 103 cars = (ProductLine) list.get(0); 104 oss = (ProductLine) list.get(1); 105 assertEquals( cars.getModels().size(), 2 ); 106 assertEquals( oss.getModels().size(), 3 ); 107 s.delete(cars); 108 s.delete(oss); 109 t.commit(); 110 s.close(); 111 } 112 113 114 protected String [] getMappings() { 115 return new String [] { "batchfetch/ProductLine.hbm.xml" }; 116 } 117 118 public static Test suite() { 119 return new TestSuite(BatchFetchTest.class); 120 } 121 122 } 123 124 | Popular Tags |