1 package org.hibernate.test.ops; 3 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 7 import org.hibernate.Hibernate; 8 import org.hibernate.Session; 9 import org.hibernate.Transaction; 10 import org.hibernate.cfg.Configuration; 11 import org.hibernate.cfg.Environment; 12 import org.hibernate.test.TestCase; 13 14 17 public class GetLoadTest extends TestCase { 18 19 public GetLoadTest(String str) { 20 super(str); 21 } 22 23 public void testGetLoad() { 24 clearCounts(); 25 26 Session s = openSession(); 27 Transaction tx = s.beginTransaction(); 28 Employer emp = new Employer(); 29 s.persist(emp); 30 Node node = new Node("foo"); 31 Node parent = new Node("bar"); 32 parent.addChild(node); 33 s.persist(parent); 34 tx.commit(); 35 s.close(); 36 37 s = openSession(); 38 tx = s.beginTransaction(); 39 emp = (Employer) s.get(Employer.class, emp.getId()); 40 assertTrue( Hibernate.isInitialized(emp) ); 41 assertFalse( Hibernate.isInitialized(emp.getEmployees()) ); 42 node = (Node) s.get(Node.class, node.getName()); 43 assertTrue( Hibernate.isInitialized(node) ); 44 assertFalse( Hibernate.isInitialized(node.getChildren()) ); 45 assertFalse( Hibernate.isInitialized(node.getParent()) ); 46 assertNull( s.get(Node.class, "xyz") ); 47 tx.commit(); 48 s.close(); 49 50 s = openSession(); 51 tx = s.beginTransaction(); 52 emp = (Employer) s.load(Employer.class, emp.getId()); 53 emp.getId(); 54 assertFalse( Hibernate.isInitialized(emp) ); 55 node = (Node) s.load(Node.class, node.getName()); 56 assertEquals( node.getName(), "foo" ); 57 assertFalse( Hibernate.isInitialized(node) ); 58 tx.commit(); 59 s.close(); 60 61 s = openSession(); 62 tx = s.beginTransaction(); 63 emp = (Employer) s.get("org.hibernate.test.ops.Employer", emp.getId()); 64 assertTrue( Hibernate.isInitialized(emp) ); 65 node = (Node) s.get("org.hibernate.test.ops.Node", node.getName()); 66 assertTrue( Hibernate.isInitialized(node) ); 67 tx.commit(); 68 s.close(); 69 70 s = openSession(); 71 tx = s.beginTransaction(); 72 emp = (Employer) s.load("org.hibernate.test.ops.Employer", emp.getId()); 73 emp.getId(); 74 assertFalse( Hibernate.isInitialized(emp) ); 75 node = (Node) s.load("org.hibernate.test.ops.Node", node.getName()); 76 assertEquals( node.getName(), "foo" ); 77 assertFalse( Hibernate.isInitialized(node) ); 78 tx.commit(); 79 s.close(); 80 81 assertFetchCount(0); 82 } 83 84 private void clearCounts() { 85 getSessions().getStatistics().clear(); 86 } 87 88 private void assertFetchCount(int count) { 89 int fetches = (int) getSessions().getStatistics().getEntityFetchCount(); 90 assertEquals(count, fetches); 91 } 92 93 protected void configure(Configuration cfg) { 94 cfg.setProperty(Environment.GENERATE_STATISTICS, "true"); 95 cfg.setProperty(Environment.STATEMENT_BATCH_SIZE, "0"); 96 } 97 98 protected String [] getMappings() { 99 return new String [] { 100 "ops/Node.hbm.xml", 101 "ops/Employer.hbm.xml" 102 }; 103 } 104 105 public static Test suite() { 106 return new TestSuite(GetLoadTest.class); 107 } 108 109 public String getCacheConcurrencyStrategy() { 110 return null; 111 } 112 113 } 114 115 | Popular Tags |