1 package org.hibernate.test.annotations; 3 4 import java.util.List ; 5 import java.util.Date ; 6 7 import org.hibernate.HibernateException; 8 import org.hibernate.Session; 9 import org.hibernate.StaleStateException; 10 import org.hibernate.Transaction; 11 import org.hibernate.Hibernate; 12 import org.hibernate.Query; 13 14 17 public class EntityTest extends TestCase { 18 19 public EntityTest(String x) { 20 super(x); 21 } 22 23 public void testLoad() throws Exception { 24 Session s = openSession(); 26 Transaction tx = s.beginTransaction(); 27 Flight firstOne = new Flight(); 28 firstOne.setId( new Long (1) ); 29 firstOne.setName("AF3202"); 30 firstOne.setDuration( new Long (1000000) ); 31 firstOne.setDurationInSec(2000); 32 s.save(firstOne); 33 s.flush(); 34 tx.commit(); 35 s.close(); 36 37 s = openSession(); 39 tx = s.beginTransaction(); 40 firstOne = (Flight) s.get( Flight.class, new Long (1) ); 41 assertNotNull(firstOne); 42 assertEquals( new Long (1), firstOne.getId() ); 43 assertEquals( "AF3202", firstOne.getName() ); 44 assertEquals( new Long (1000000), firstOne.getDuration() ); 45 assertFalse( "Transient is not working", 2000l == firstOne.getDurationInSec() ); 46 tx.commit(); 47 s.close(); 48 } 49 50 public void testColumn() throws Exception { 51 Session s = openSession(); 53 Transaction tx = s.beginTransaction(); 54 Flight firstOne = new Flight(); 55 firstOne.setId( new Long (1) ); 56 firstOne.setName(null); 57 58 try { 59 s.save(firstOne); 60 tx.commit(); 61 fail("Name column should be not null"); 62 } catch (HibernateException e) { 63 } 65 finally { 66 s.close(); 67 } 68 69 s = openSession(); 71 tx = s.beginTransaction(); 72 firstOne = new Flight(); 73 firstOne.setId( new Long (1) ); 74 firstOne.setName("AF3202"); 75 firstOne.setTriggeredData("should not be insertable"); 76 tx.commit(); 77 s.close(); 78 79 s = openSession(); 80 tx = s.beginTransaction(); 81 firstOne = (Flight) s.get( Flight.class, new Long (1) ); 82 assertNotNull(firstOne); 83 assertEquals( new Long (1), firstOne.getId() ); 84 assertEquals( "AF3202", firstOne.getName() ); 85 assertFalse( "should not be insertable".equals( firstOne.getTriggeredData() ) ); 86 firstOne.setName("BA1234"); 87 firstOne.setTriggeredData("should not be updatable"); 88 tx.commit(); 89 s.close(); 90 91 s = openSession(); 92 tx = s.beginTransaction(); 93 firstOne = (Flight) s.get( Flight.class, new Long (1) ); 94 assertNotNull(firstOne); 95 assertEquals( new Long (1), firstOne.getId() ); 96 assertEquals( "AF3202", firstOne.getName() ); 97 assertFalse( "should not be updatable".equals( firstOne.getTriggeredData() ) ); 98 tx.commit(); 99 s.close(); 100 } 101 102 public void testColumnUnique() throws Exception { 103 Session s; 104 Transaction tx; 105 s = openSession(); 106 tx = s.beginTransaction(); 107 Sky sky = new Sky(); 108 sky.id = new Long (2); 109 sky.color = "blue"; 110 sky.day = "monday"; 111 sky.month = "January"; 112 113 Sky sameSky = new Sky(); 114 sameSky.id = new Long (3); 115 sameSky.color = "blue"; 116 sky.day = "tuesday"; 117 sky.month="January"; 118 119 try { 120 s.save(sky); 121 s.flush(); 122 s.save(sameSky); 123 tx.commit(); 124 fail("unique constraints not respected"); 125 } 126 catch(HibernateException e) { 127 } finally { 129 if (tx != null) tx.rollback(); 130 s.close(); 131 } 132 } 133 134 public void testUniqueConstraint() throws Exception { 135 int id=5; 136 Session s; 137 Transaction tx; 138 s = openSession(); 139 tx = s.beginTransaction(); 140 Sky sky = new Sky(); 141 sky.id = new Long (id++); 142 sky.color = "green"; 143 sky.day = "monday"; 144 sky.month="March"; 145 146 Sky otherSky = new Sky(); 147 otherSky.id = new Long (id++); 148 otherSky.color = "red"; 149 otherSky.day = "friday"; 150 otherSky.month="March"; 151 152 Sky sameSky = new Sky(); 153 sameSky.id = new Long (id++); 154 sameSky.color = "green"; 155 sameSky.day = "monday"; 156 sameSky.month="March"; 157 158 s.save(sky); 159 s.flush(); 160 161 s.save(otherSky); 162 tx.commit(); 163 s.close(); 164 165 s = openSession(); 166 tx = s.beginTransaction(); 167 try { 168 s.save(sameSky); 169 tx.commit(); 170 fail("unique constraints not respected"); 171 } 172 catch(HibernateException e) { 173 } finally { 175 if (tx != null) tx.rollback(); 176 s.close(); 177 } 178 } 179 180 public void testVersion() throws Exception { 181 Session s = openSession(); 183 Transaction tx = s.beginTransaction(); 184 Flight firstOne = new Flight(); 185 firstOne.setId( new Long (2) ); 186 firstOne.setName("AF3202"); 187 firstOne.setDuration( new Long (500) ); 188 s.save(firstOne); 189 s.flush(); 190 tx.commit(); 191 s.close(); 192 193 s = openSession(); 195 tx = s.beginTransaction(); 196 firstOne = (Flight) s.get( Flight.class, new Long (2) ); 197 tx.commit(); 198 s.close(); 199 200 s = openSession(); 202 tx = s.beginTransaction(); 203 Flight concurrentOne = (Flight) s.get( Flight.class, new Long (2) ); 204 concurrentOne.setDuration( new Long (1000) ); 205 s.update(concurrentOne); 206 tx.commit(); 207 s.close(); 208 assertFalse(firstOne == concurrentOne); 209 assertFalse( firstOne.getVersion().equals( concurrentOne.getVersion() ) ); 210 211 s = openSession(); 213 tx = s.beginTransaction(); 214 firstOne.setName("Second access"); 215 s.update(firstOne); 216 try { 217 tx.commit(); 218 fail("Optimistic locking should work"); 219 } 220 catch (StaleStateException e) { 221 } 223 finally { 224 if (tx != null) tx.rollback(); 225 s.close(); 226 } 227 228 } 229 230 public void testFieldAccess() throws Exception { 231 Session s; 232 Transaction tx; 233 s = openSession(); 234 tx = s.beginTransaction(); 235 Sky sky = new Sky(); 236 sky.id = new Long (1); 237 sky.color = "black"; 238 Sky.area = "Paris"; 239 s.save(sky); 240 tx.commit(); 241 s.close(); 242 Sky.area = "London"; 243 244 s = openSession(); 245 tx = s.beginTransaction(); 246 sky = (Sky) s.get(Sky.class, sky.id); 247 assertNotNull(sky); 248 assertEquals("black", sky.color); 249 assertFalse("Paris".equals( Sky.area ) ); 250 tx.commit(); 251 s.close(); 252 } 253 254 public void testEntityName() throws Exception { 255 Session s = openSession(); 256 Transaction tx = s.beginTransaction(); 257 Company comp = new Company(); 258 s.persist(comp); 259 comp.setName("JBoss Inc"); 260 tx.commit(); 261 s.close(); 262 263 s = openSession(); 264 tx = s.beginTransaction(); 265 List result = s.createQuery("from Corporation").list(); 266 assertNotNull(result); 267 assertEquals( 1, result.size() ); 268 tx.commit(); 269 s.close(); 270 271 } 272 273 public void testNonGetter() throws Exception { 274 Session s = openSession(); 275 Transaction tx = s.beginTransaction(); 276 Flight airFrance = new Flight(); 277 airFrance.setId( new Long (747) ); 278 airFrance.setName("Paris-Amsterdam"); 279 airFrance.setDuration( new Long (10) ); 280 airFrance.setFactor(25); 281 s.persist(airFrance); 282 tx.commit(); 283 s.close(); 284 285 s = openSession(); 286 tx = s.beginTransaction(); 287 airFrance = (Flight) s.get( Flight.class, airFrance.getId() ); 288 assertNotNull(airFrance); 289 assertEquals( new Long (10), airFrance.getDuration() ); 290 assertFalse( 25 == airFrance.getFactor(false) ); 291 s.delete(airFrance); 292 tx.commit(); 293 s.close(); 294 } 295 296 public void testFormula() throws Exception { 297 Session s = openSession(); 298 Transaction tx = s.beginTransaction(); 299 Flight airFrance = new Flight(); 300 airFrance.setId( new Long (747) ); 301 airFrance.setName("Paris-Amsterdam"); 302 airFrance.setDuration( new Long (10) ); 303 airFrance.setMaxAltitude(10000); 304 s.persist(airFrance); 305 tx.commit(); 306 s.close(); 307 308 s = openSession(); 309 tx = s.beginTransaction(); 310 airFrance = (Flight) s.get( Flight.class, airFrance.getId() ); 311 assertNotNull(airFrance); 312 assertEquals( new Long (10), airFrance.getDuration() ); 313 assertEquals( 10000000, airFrance.getMaxAltitudeInMilimeter() ); 314 s.delete(airFrance); 315 tx.commit(); 316 s.close(); 317 } 318 319 public void testTemporalType() throws Exception { 320 Session s = openSession(); 321 Transaction tx = s.beginTransaction(); 322 Flight airFrance = new Flight(); 323 airFrance.setId( new Long (747) ); 324 airFrance.setName("Paris-Amsterdam"); 325 airFrance.setDuration( new Long (10) ); 326 airFrance.setDepartureDate( new Date (05, 06, 21, 10, 0, 0) ); 327 airFrance.setFactor(25); 328 s.persist(airFrance); 329 tx.commit(); 330 s.close(); 331 332 s = openSession(); 333 tx = s.beginTransaction(); 334 Query q = s.createQuery("from Flight f where f.departureDate = :departureDate"); 335 q.setParameter("departureDate", airFrance.getDepartureDate(), Hibernate.DATE); 336 Flight copyAirFrance = (Flight) q.uniqueResult(); 337 assertNotNull(airFrance); 338 assertEquals( 339 new Date (05, 06, 21), 340 copyAirFrance.getDepartureDate() 341 ); 342 s.delete(copyAirFrance); 343 tx.commit(); 344 s.close(); 345 } 346 347 public void testBasic() throws Exception { 348 Session s = openSession(); 349 Transaction tx = s.beginTransaction(); 350 Flight airFrance = new Flight(); 351 airFrance.setId( new Long (747) ); 352 airFrance.setName("Paris-Amsterdam"); 353 airFrance.setDuration( null ); 354 airFrance.setMaxAltitude(10000); 355 try { 356 s.persist(airFrance); 357 tx.commit(); 358 fail("Basic(optional=false) fails"); 359 } 360 catch (Exception e) { 361 if (tx != null) tx.rollback(); 363 } 364 finally { 365 s.close(); 366 } 367 } 368 369 372 protected Class [] getMappings() { 373 return new Class [] { 374 Flight.class, 375 Company.class, 376 Sky.class 377 }; 378 } 379 380 } 381 382 | Popular Tags |