1 package org.hibernate.ejb.test.ops; 3 4 import java.util.ArrayList ; 5 import java.util.Collection ; 6 7 import junit.framework.Test; 8 import junit.framework.TestSuite; 9 import org.hibernate.PersistentObjectException; 10 import org.hibernate.Session; 11 import org.hibernate.Transaction; 12 import org.hibernate.cfg.Configuration; 13 import org.hibernate.cfg.Environment; 14 import org.hibernate.ejb.test.EJB3TestCase; 15 import org.hibernate.exception.ConstraintViolationException; 16 17 20 public class CreateTest extends EJB3TestCase { 21 22 public CreateTest(String str) { 23 super( str ); 24 } 25 26 public void testCreateTree() { 27 28 clearCounts(); 29 30 Session s = openSession(); 31 Transaction tx = s.beginTransaction(); 32 Node root = new Node( "root" ); 33 Node child = new Node( "child" ); 34 root.addChild( child ); 35 s.persist( root ); 36 tx.commit(); 37 s.close(); 38 39 assertInsertCount( 2 ); 40 assertUpdateCount( 0 ); 41 42 s = openSession(); 43 tx = s.beginTransaction(); 44 root = (Node) s.get( Node.class, "root" ); 45 Node child2 = new Node( "child2" ); 46 root.addChild( child2 ); 47 tx.commit(); 48 s.close(); 49 50 assertInsertCount( 3 ); 51 assertUpdateCount( 0 ); 52 } 53 54 public void testCreateTreeWithGeneratedId() { 55 56 clearCounts(); 57 58 Session s = openSession(); 59 Transaction tx = s.beginTransaction(); 60 NumberedNode root = new NumberedNode( "root" ); 61 NumberedNode child = new NumberedNode( "child" ); 62 root.addChild( child ); 63 s.persist( root ); 64 tx.commit(); 65 s.close(); 66 67 assertInsertCount( 2 ); 68 assertUpdateCount( 0 ); 69 70 s = openSession(); 71 tx = s.beginTransaction(); 72 root = (NumberedNode) s.get( NumberedNode.class, new Long ( root.getId() ) ); 73 NumberedNode child2 = new NumberedNode( "child2" ); 74 root.addChild( child2 ); 75 tx.commit(); 76 s.close(); 77 78 assertInsertCount( 3 ); 79 assertUpdateCount( 0 ); 80 } 81 82 public void testCreateException() { 83 Session s = openSession(); 84 Transaction tx = s.beginTransaction(); 85 Node dupe = new Node( "dupe" ); 86 s.persist( dupe ); 87 s.persist( dupe ); 88 tx.commit(); 89 s.close(); 90 91 s = openSession(); 92 tx = s.beginTransaction(); 93 s.persist( dupe ); 94 try { 95 tx.commit(); 96 assertFalse( true ); 97 } 98 catch (ConstraintViolationException cve) { 99 } 101 tx.rollback(); 102 s.close(); 103 104 Node nondupe = new Node( "nondupe" ); 105 nondupe.addChild( dupe ); 106 107 s = openSession(); 108 tx = s.beginTransaction(); 109 s.persist( nondupe ); 110 try { 111 tx.commit(); 112 assertFalse( true ); 113 } 114 catch (ConstraintViolationException cve) { 115 } 117 tx.rollback(); 118 s.close(); 119 } 120 121 public void testCreateExceptionWithGeneratedId() { 122 Session s = openSession(); 123 Transaction tx = s.beginTransaction(); 124 NumberedNode dupe = new NumberedNode( "dupe" ); 125 s.persist( dupe ); 126 s.persist( dupe ); 127 tx.commit(); 128 s.close(); 129 130 s = openSession(); 131 tx = s.beginTransaction(); 132 try { 133 s.persist( dupe ); 134 assertFalse( true ); 135 } 136 catch (PersistentObjectException poe) { 137 } 139 tx.rollback(); 140 s.close(); 141 142 NumberedNode nondupe = new NumberedNode( "nondupe" ); 143 nondupe.addChild( dupe ); 144 145 s = openSession(); 146 tx = s.beginTransaction(); 147 try { 148 s.persist( nondupe ); 149 assertFalse( true ); 150 } 151 catch (PersistentObjectException poe) { 152 } 154 tx.rollback(); 155 s.close(); 156 } 157 158 public void testBasic() throws Exception { 159 Session s; 160 Transaction tx; 161 s = openSession(); 162 tx = s.beginTransaction(); 163 Employer er = new Employer(); 164 Employee ee = new Employee(); 165 s.persist( ee ); 166 Collection erColl = new ArrayList (); 167 Collection eeColl = new ArrayList (); 168 erColl.add( ee ); 169 eeColl.add( er ); 170 er.setEmployees( erColl ); 171 ee.setEmployers( eeColl ); 172 tx.commit(); 173 s.close(); 174 175 s = openSession(); 176 tx = s.beginTransaction(); 177 er = (Employer) s.load( Employer.class, er.getId() ); 178 assertNotNull( er ); 179 assertNotNull( er.getEmployees() ); 180 assertEquals( 1, er.getEmployees().size() ); 181 Employee eeFromDb = (Employee) er.getEmployees().iterator().next(); 182 assertEquals( ee.getId(), eeFromDb.getId() ); 183 tx.commit(); 184 s.close(); 185 } 186 187 private void clearCounts() { 188 getSessions().getStatistics().clear(); 189 } 190 191 private void assertInsertCount(int count) { 192 int inserts = (int) getSessions().getStatistics().getEntityInsertCount(); 193 assertEquals( count, inserts ); 194 } 195 196 private void assertUpdateCount(int count) { 197 int updates = (int) getSessions().getStatistics().getEntityUpdateCount(); 198 assertEquals( count, updates ); 199 } 200 201 protected void configure(Configuration cfg) { 202 super.configure( cfg ); 203 cfg.setProperty( Environment.GENERATE_STATISTICS, "true" ); 204 cfg.setProperty( Environment.STATEMENT_BATCH_SIZE, "0" ); 205 } 206 207 protected String [] getMappings() { 208 return new String []{ 209 "ops/Node.hbm.xml", 210 "ops/Employer.hbm.xml" 211 }; 212 } 213 214 public static Test suite() { 215 return new TestSuite( CreateTest.class ); 216 } 217 218 public String getCacheConcurrencyStrategy() { 219 return null; 220 } 221 222 } 223 224 | Popular Tags |