1 package org.hibernate.ejb.test.ops; 3 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 import org.hibernate.Hibernate; 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 import org.hibernate.cfg.Configuration; 10 import org.hibernate.cfg.Environment; 11 import org.hibernate.ejb.test.EJB3TestCase; 12 13 16 public class GetLoadTest extends EJB3TestCase { 17 18 public GetLoadTest(String str) { 19 super( str ); 20 } 21 22 public void testGetLoad() { 23 clearCounts(); 24 25 Session s = openSession(); 26 Transaction tx = s.beginTransaction(); 27 Employer emp = new Employer(); 28 s.persist( emp ); 29 Node node = new Node( "foo" ); 30 Node parent = new Node( "bar" ); 31 parent.addChild( node ); 32 s.persist( parent ); 33 tx.commit(); 34 s.close(); 35 36 s = openSession(); 37 tx = s.beginTransaction(); 38 emp = (Employer) s.get( Employer.class, emp.getId() ); 39 assertTrue( Hibernate.isInitialized( emp ) ); 40 assertFalse( Hibernate.isInitialized( emp.getEmployees() ) ); 41 node = (Node) s.get( Node.class, node.getName() ); 42 assertTrue( Hibernate.isInitialized( node ) ); 43 assertFalse( Hibernate.isInitialized( node.getChildren() ) ); 44 assertFalse( Hibernate.isInitialized( node.getParent() ) ); 45 assertNull( s.get( Node.class, "xyz" ) ); 46 tx.commit(); 47 s.close(); 48 49 s = openSession(); 50 tx = s.beginTransaction(); 51 emp = (Employer) s.load( Employer.class, emp.getId() ); 52 emp.getId(); 53 assertFalse( Hibernate.isInitialized( emp ) ); 54 node = (Node) s.load( Node.class, node.getName() ); 55 assertEquals( node.getName(), "foo" ); 56 assertFalse( Hibernate.isInitialized( node ) ); 57 tx.commit(); 58 s.close(); 59 60 s = openSession(); 61 tx = s.beginTransaction(); 62 emp = (Employer) s.get( "org.hibernate.ejb.test.ops.Employer", emp.getId() ); 63 assertTrue( Hibernate.isInitialized( emp ) ); 64 node = (Node) s.get( "org.hibernate.ejb.test.ops.Node", node.getName() ); 65 assertTrue( Hibernate.isInitialized( node ) ); 66 tx.commit(); 67 s.close(); 68 69 s = openSession(); 70 tx = s.beginTransaction(); 71 emp = (Employer) s.load( "org.hibernate.ejb.test.ops.Employer", emp.getId() ); 72 emp.getId(); 73 assertFalse( Hibernate.isInitialized( emp ) ); 74 node = (Node) s.load( "org.hibernate.ejb.test.ops.Node", node.getName() ); 75 assertEquals( node.getName(), "foo" ); 76 assertFalse( Hibernate.isInitialized( node ) ); 77 tx.commit(); 78 s.close(); 79 80 assertFetchCount( 0 ); 81 } 82 83 private void clearCounts() { 84 getSessions().getStatistics().clear(); 85 } 86 87 private void assertFetchCount(int count) { 88 int fetches = (int) getSessions().getStatistics().getEntityFetchCount(); 89 assertEquals( count, fetches ); 90 } 91 92 protected void configure(Configuration cfg) { 93 super.configure( 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 |