1 package org.hibernate.test.annotations.id; 3 4 import org.hibernate.Session; 5 import org.hibernate.Transaction; 6 import org.hibernate.test.annotations.TestCase; 7 8 11 public class IdTest extends TestCase { 12 14 public IdTest(String x) { 15 super(x); 16 } 17 18 public void testGenericGenerator() throws Exception { 19 Session s = openSession(); 20 Transaction tx = s.beginTransaction(); 21 SoundSystem system = new SoundSystem(); 22 system.setBrand( "Genelec" ); 23 system.setModel( "T234" ); 24 Furniture fur = new Furniture(); 25 s.persist( system ); 26 s.persist( fur ); 27 tx.commit(); 28 s.close(); 29 30 s = openSession(); 31 tx = s.beginTransaction(); 32 system = (SoundSystem) s.get( SoundSystem.class, system.getId() ); 33 fur = (Furniture) s.get(Furniture.class, fur.getId() ); 34 assertNotNull( system ); 35 assertNotNull( fur ); 36 s.delete( system ); 37 s.delete( fur ); 38 tx.commit(); 39 s.close(); 40 41 } 42 43 public void testTableGenerator() throws Exception { 44 Session s = openSession(); 45 Transaction tx = s.beginTransaction(); 46 Ball b = new Ball(); 47 Dog d = new Dog(); 48 Computer c = new Computer(); 49 s.persist(b); 50 s.persist(d); 51 s.persist(c); 52 tx.commit(); 53 s.close(); 54 assertEquals( "table id not generated", new Integer (1), b.getId() ); 55 assertEquals( "generator should not be shared", new Integer (1), d.getId() ); 56 assertEquals( "default value should work", new Long (1), c.getId() ); 57 58 s = openSession(); 59 tx = s.beginTransaction(); 60 s.delete( s.get( Ball.class, new Integer (1) ) ); 61 s.delete( s.get( Dog.class, new Integer (1) ) ); 62 s.delete( s.get( Computer.class, new Long (1) ) ); 63 tx.commit(); 64 s.close(); 65 } 66 67 public void testSequenceGenerator() throws Exception { 68 Session s = openSession(); 69 Transaction tx = s.beginTransaction(); 70 Shoe b = new Shoe(); 71 s.persist(b); 72 tx.commit(); 73 s.close(); 74 assertNotNull( b.getId() ); 75 76 s = openSession(); 77 tx = s.beginTransaction(); 78 s.delete( s.get( Shoe.class, b.getId() ) ); 79 tx.commit(); 80 s.close(); 81 } 82 83 public void testClassLevelGenerator() throws Exception { 84 Session s = openSession(); 85 Transaction tx = s.beginTransaction(); 86 Store b = new Store(); 87 s.persist(b); 88 tx.commit(); 89 s.close(); 90 assertNotNull( b.getId() ); 91 92 s = openSession(); 93 tx = s.beginTransaction(); 94 s.delete( s.get( Store.class, b.getId() ) ); 95 tx.commit(); 96 s.close(); 97 } 98 99 public void testMethodLevelGenerator() throws Exception { 100 Session s = openSession(); 101 Transaction tx = s.beginTransaction(); 102 Department b = new Department(); 103 s.persist(b); 104 tx.commit(); 105 s.close(); 106 assertNotNull( b.getId() ); 107 108 s = openSession(); 109 tx = s.beginTransaction(); 110 s.delete( s.get( Department.class, b.getId() ) ); 111 tx.commit(); 112 s.close(); 113 } 114 115 public void testDefaultSequence() throws Exception { 116 Session s; 117 Transaction tx; 118 s = openSession(); 119 tx = s.beginTransaction(); 120 Home h = new Home(); 121 s.persist(h); 122 tx.commit(); 123 s.close(); 124 assertNotNull( h.getId() ); 125 126 s = openSession(); 127 tx = s.beginTransaction(); 128 Home reloadedHome = (Home) s.get( Home.class, h.getId() ); 129 assertEquals( h.getId(), reloadedHome.getId() ); 130 s.delete(reloadedHome); 131 tx.commit(); 132 s.close(); 133 } 134 135 public void testParameterizedAuto() throws Exception { 136 Session s; 137 Transaction tx; 138 s = openSession(); 139 tx = s.beginTransaction(); 140 Home h = new Home(); 141 s.persist(h); 142 tx.commit(); 143 s.close(); 144 assertNotNull( h.getId() ); 145 146 s = openSession(); 147 tx = s.beginTransaction(); 148 Home reloadedHome = (Home) s.get( Home.class, h.getId() ); 149 assertEquals( h.getId(), reloadedHome.getId() ); 150 s.delete(reloadedHome); 151 tx.commit(); 152 s.close(); 153 } 154 155 public void testIdInEmbeddableSuperclass() throws Exception { 156 Session s; 157 Transaction tx; 158 s = openSession(); 159 tx = s.beginTransaction(); 160 FirTree chrismasTree = new FirTree(); 161 s.persist(chrismasTree); 162 tx.commit(); 163 s.clear(); 164 tx = s.beginTransaction(); 165 chrismasTree = (FirTree) s.get( FirTree.class, chrismasTree.getId() ); 166 assertNotNull( chrismasTree ); 167 s.delete( chrismasTree ); 168 tx.commit(); 169 s.close(); 170 } 171 172 public void testIdClass() throws Exception { 173 Session s; 174 Transaction tx; 175 s = openSession(); 176 tx = s.beginTransaction(); 177 Footballer fb = new Footballer("David", "Beckam", "Arsenal"); 178 s.persist(fb); 179 tx.commit(); 180 s.clear(); 181 tx = s.beginTransaction(); 182 FootballerPk fpk = new FootballerPk("David", "Beckam"); 183 fb = (Footballer) s.get(Footballer.class, fpk); 184 assertNotNull(fb); 185 assertEquals( "Beckam", fb.getLastname() ); 186 assertEquals( "Arsenal", fb.getClub() ); 187 s.delete( fb ); 188 tx.commit(); 189 s.close(); 190 } 191 192 195 protected Class [] getMappings() { 196 return new Class [] { 197 Ball.class, 198 Shoe.class, 199 Store.class, 200 Department.class, 201 Dog.class, 202 Computer.class, 203 Home.class, 204 Phone.class, 205 Tree.class, 206 FirTree.class, 207 Footballer.class, 208 SoundSystem.class, 209 Furniture.class 210 }; 211 } 212 213 216 protected String [] getAnnotatedPackages() { 217 return new String [] { 218 "org.hibernate.test.annotations" 219 }; 220 } 221 222 } 223 | Popular Tags |