| 1 package org.hibernate.test.legacy; 3 4 import java.io.Serializable ; 5 import java.sql.Connection ; 6 import java.sql.Time ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Collections ; 10 import java.util.Date ; 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Locale ; 16 import java.util.Set ; 17 import java.util.SortedSet ; 18 import java.util.TimeZone ; 19 import java.util.TreeMap ; 20 import java.util.TreeSet ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 import org.hibernate.Criteria; 27 import org.hibernate.FetchMode; 28 import org.hibernate.FlushMode; 29 import org.hibernate.Hibernate; 30 import org.hibernate.HibernateException; 31 import org.hibernate.LazyInitializationException; 32 import org.hibernate.LockMode; 33 import org.hibernate.ObjectDeletedException; 34 import org.hibernate.ObjectNotFoundException; 35 import org.hibernate.Query; 36 import org.hibernate.QueryException; 37 import org.hibernate.ScrollableResults; 38 import org.hibernate.Transaction; 39 import org.hibernate.cfg.Configuration; 40 import org.hibernate.cfg.Environment; 41 import org.hibernate.classic.Session; 42 import org.hibernate.connection.ConnectionProvider; 43 import org.hibernate.connection.DriverManagerConnectionProvider; 44 import org.hibernate.criterion.Example; 45 import org.hibernate.criterion.Expression; 46 import org.hibernate.criterion.MatchMode; 47 import org.hibernate.criterion.Order; 48 import org.hibernate.dialect.DB2Dialect; 49 import org.hibernate.dialect.DerbyDialect; 50 import org.hibernate.dialect.HSQLDialect; 51 import org.hibernate.dialect.InterbaseDialect; 52 import org.hibernate.dialect.MckoiDialect; 53 import org.hibernate.dialect.MySQLDialect; 54 import org.hibernate.dialect.Oracle9Dialect; 55 import org.hibernate.dialect.OracleDialect; 56 import org.hibernate.dialect.PointbaseDialect; 57 import org.hibernate.dialect.PostgreSQLDialect; 58 import org.hibernate.dialect.SAPDBDialect; 59 import org.hibernate.dialect.SQLServerDialect; 60 import org.hibernate.dialect.SybaseDialect; 61 import org.hibernate.dialect.TimesTenDialect; 62 import org.hibernate.engine.SessionFactoryImplementor; 63 import org.hibernate.jmx.HibernateService; 64 import org.hibernate.mapping.RootClass; 65 import org.hibernate.proxy.HibernateProxy; 66 import org.hibernate.test.TestCase; 67 import org.hibernate.type.Type; 68 import org.hibernate.util.JoinedIterator; 69 import org.hibernate.util.SerializationHelper; 70 71 public class FooBarTest extends TestCase { 72 73 public FooBarTest(String arg) { 74 super(arg); 75 } 76 77 public void testSaveOrUpdateCopyAny() throws Exception { 78 Session s = openSession(); 79 Bar bar = new Bar(); 80 One one = new One(); 81 bar.setObject(one); 82 s.save(bar); 83 GlarchProxy g = bar.getComponent().getGlarch(); 84 bar.getComponent().setGlarch(null); 85 s.delete(g); 86 s.flush(); 87 assertTrue( s.contains(one) ); 88 s.connection().commit(); 89 s.close(); 90 91 s = openSession(); 92 Bar bar2 = (Bar) s.saveOrUpdateCopy(bar); 93 s.flush(); 94 s.delete(bar2); 95 s.flush(); 96 s.connection().commit(); 97 s.close(); 98 } 99 100 public void testRefreshProxy() throws Exception { 101 Session s = openSession(); 102 Glarch g = new Glarch(); 103 Serializable gid = s.save(g); 104 s.flush(); 105 s.clear(); 106 GlarchProxy gp = (GlarchProxy) s.load(Glarch.class, gid); 107 gp.getName(); s.refresh(gp); 109 s.delete(gp); 110 s.flush(); 111 s.connection().commit(); 112 s.close(); 113 } 114 115 public void testOnCascadeDelete() throws Exception { 116 117 if (getDialect() instanceof MySQLDialect) return; 118 119 Session s = openSession(); 120 Baz baz = new Baz(); 121 baz.subs = new ArrayList (); 122 Baz sub = new Baz(); 123 sub.superBaz = baz; 124 baz.subs.add(sub); 125 s.save(baz); 126 s.flush(); 127 assertTrue( s.createQuery("from Baz").list().size()==2 ); 128 s.connection().commit(); 129 s.delete(baz); 130 s.flush(); 131 s.connection().commit(); 132 assertTrue( s.createQuery("from Baz").list().size()==0 ); 133 s.connection().commit(); 134 s.close(); 135 } 136 137 public void testRemoveFromIdbag() throws Exception { 138 Session s = openSession(); 139 Baz baz = new Baz(); 140 baz.setByteBag( new ArrayList () ); 141 byte[] bytes = { 12, 13 }; 142 baz.getByteBag().add( new byte[] { 10, 45 } ); 143 baz.getByteBag().add(bytes); 144 baz.getByteBag().add( new byte[] { 1, 11 } ); 145 baz.getByteBag().add( new byte[] { 12 } ); 146 s.save(baz); 147 s.flush(); 148 baz.getByteBag().remove(bytes); 149 s.flush(); 150 baz.getByteBag().add(bytes); 151 s.flush(); 152 s.delete(baz); 153 s.flush(); 154 s.connection().commit(); 155 s.close(); 156 } 157 158 public void testLoad() throws Exception { 159 Session s = openSession(); 160 Qux q = new Qux(); 161 s.save(q); 162 BarProxy b = new Bar(); 163 s.save(b); 164 s.flush(); 165 s.connection().commit(); 166 s.close(); 167 s = openSession(); 168 q = (Qux) s.load(Qux.class, q.getKey() ); 169 b = (BarProxy) s.load( Foo.class, b.getKey() ); 170 b.getKey(); 171 assertFalse( Hibernate.isInitialized(b) ); 172 b.getBarString(); 173 assertTrue( Hibernate.isInitialized(b) ); 174 BarProxy b2 = (BarProxy) s.load( Bar.class, new String ( b.getKey() ) ); 175 Qux q2 = (Qux) s.load( Qux.class, q.getKey() ); 176 assertTrue( "loaded same object", q==q2 ); 177 assertTrue( "loaded same object", b==b2 ); 178 assertTrue( Math.round( b.getFormula() ) == b.getInt()/2 ); 179 s.delete(q2); 180 s.delete(b2); 181 s.flush(); 182 s.connection().commit(); 183 s.close(); 184 } 185 186 public void testJoin() throws Exception { 187 Session s = openSession(); 188 Foo foo = new Foo(); 189 foo.setJoinedProp("foo"); 190 s.save(foo); 191 s.flush(); 192 foo.setJoinedProp("bar"); 193 s.flush(); 194 String fid = foo.getKey(); 195 s.delete(foo); 196 s.flush(); 197 s.connection().commit(); 198 s.close(); 199 200 s = openSession(); 201 Foo foo2 = new Foo(); 202 foo2.setJoinedProp("foo"); 203 s.save(foo2); 204 s.find("select foo.id from Foo foo where foo.joinedProp = 'foo'"); 205 assertNull( s.get(Foo.class, fid) ); 206 s.delete(foo2); 207 s.flush(); 208 s.connection().commit(); 209 s.close(); 210 211 } 212 213 public void testDereferenceLazyCollection() throws Exception { 214 Session s = openSession(); 215 Baz baz = new Baz(); 216 baz.setFooSet( new HashSet () ); 217 Foo foo = new Foo(); 218 baz.getFooSet().add(foo); 219 s.save(foo); 220 s.save(baz); 221 foo.setBytes( "foobar".getBytes() ); 222 s.flush(); 223 s.connection().commit(); 224 s.close(); 225 226 s = openSession(); 227 foo = (Foo) s.get( Foo.class, foo.getKey() ); 228 assertTrue( Hibernate.isInitialized( foo.getBytes() ) ); 229 assertTrue( foo.getBytes().length==6 ); 230 baz = (Baz) s.get( Baz.class, baz.getCode() ); 231 assertTrue( baz.getFooSet().size()==1 ); 232 s.flush(); 233 s.connection().commit(); 234 s.close(); 235 236 getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet"); 237 238 s = openSession(); 239 baz = (Baz) s.get( Baz.class, baz.getCode() ); 240 assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); 241 baz.setFooSet(null); 242 s.flush(); 243 s.connection().commit(); 244 s.close(); 245 246 s = openSession(); 247 foo = (Foo) s.get( Foo.class, foo.getKey() ); 248 assertTrue( foo.getBytes().length==6 ); 249 baz = (Baz) s.get( Baz.class, baz.getCode() ); 250 assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); 251 assertTrue( baz.getFooSet().size()==0 ); 252 s.delete(baz); 253 s.delete(foo); 254 s.flush(); 255 s.connection().commit(); 256 s.close(); 257 } 258 259 public void testMoveLazyCollection() throws Exception { 260 Session s = openSession(); 261 Baz baz = new Baz(); 262 Baz baz2 = new Baz(); 263 baz.setFooSet( new HashSet () ); 264 Foo foo = new Foo(); 265 baz.getFooSet().add(foo); 266 s.save(foo); 267 s.save(baz); 268 s.save(baz2); 269 foo.setBytes( "foobar".getBytes() ); 270 s.flush(); 271 s.connection().commit(); 272 s.close(); 273 274 s = openSession(); 275 foo = (Foo) s.get( Foo.class, foo.getKey() ); 276 assertTrue( Hibernate.isInitialized( foo.getBytes() ) ); 277 assertTrue( foo.getBytes().length==6 ); 278 baz = (Baz) s.get( Baz.class, baz.getCode() ); 279 assertTrue( baz.getFooSet().size()==1 ); 280 s.flush(); 281 s.connection().commit(); 282 s.close(); 283 284 getSessions().evictCollection("org.hibernate.test.legacy.Baz.fooSet"); 285 286 s = openSession(); 287 baz = (Baz) s.get( Baz.class, baz.getCode() ); 288 assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); 289 baz2 = (Baz) s.get( Baz.class, baz2.getCode() ); 290 baz2.setFooSet( baz.getFooSet() ); 291 baz.setFooSet(null); 292 assertFalse( Hibernate.isInitialized( baz2.getFooSet() ) ); 293 s.flush(); 294 s.connection().commit(); 295 s.close(); 296 297 s = openSession(); 298 foo = (Foo) s.get( Foo.class, foo.getKey() ); 299 assertTrue( foo.getBytes().length==6 ); 300 baz = (Baz) s.get( Baz.class, baz.getCode() ); 301 baz2 = (Baz) s.get( Baz.class, baz2.getCode() ); 302 assertFalse( Hibernate.isInitialized( baz.getFooSet() ) ); 303 assertTrue( baz.getFooSet().size()==0 ); 304 assertTrue( Hibernate.isInitialized( baz2.getFooSet() ) ); assertTrue( baz2.getFooSet().size()==1 ); 306 s.delete(baz); 307 s.delete(baz2); 308 s.delete(foo); 309 s.flush(); 310 s.connection().commit(); 311 s.close(); 312 } 313 314 public void testCriteriaCollection() throws Exception { 315 Session s = openSession(); 316 Baz bb = (Baz) s.createCriteria(Baz.class).uniqueResult(); 317 assertTrue(bb==null); 318 Baz baz = new Baz(); 319 s.save(baz); 320 s.flush(); 321 s.connection().commit(); 322 s.close(); 323 s = openSession(); 324 Baz b = (Baz) s.createCriteria(Baz.class).uniqueResult(); 325 assertTrue( Hibernate.isInitialized( b.getTopGlarchez() ) ); 326 assertTrue( b.getTopGlarchez().size()==0 ); 327 s.delete(b); 328 s.flush(); 329 s.connection().commit(); 330 s.close(); 331 } 332 333 public void testQuery() throws Exception { 334 Session s = openSession(); 335 Foo foo = new Foo(); 336 s.save(foo); 337 Foo foo2 = new Foo(); 338 s.save(foo2); 339 foo.setFoo(foo2); 340 341 List list = s.find("from Foo foo inner join fetch foo.foo"); 342 Foo foof = (Foo) list.get(0); 343 assertTrue( Hibernate.isInitialized( foof.getFoo() ) ); 344 345 list = s.find("from Baz baz left outer join fetch baz.fooToGlarch"); 346 347 list = s.find( 348 "select foo, bar from Foo foo left outer join foo.foo bar where foo = ?", 349 foo, 350 Hibernate.entity(Foo.class) 351 ); 352 Object [] row1 = (Object []) list.get(0); 353 assertTrue( row1[0]==foo && row1[1]==foo2 ); 354 355 s.find("select foo.foo.foo.string from Foo foo where foo.foo = 'bar'"); 356 s.find("select foo.foo.foo.foo.string from Foo foo where foo.foo = 'bar'"); 357 s.find("select foo from Foo foo where foo.foo.foo = 'bar'"); 358 s.find("select foo.foo.foo.foo.string from Foo foo where foo.foo.foo = 'bar'"); 359 s.find("select foo.foo.foo.string from Foo foo where foo.foo.foo.foo.string = 'bar'"); 360 if ( ! (getDialect() instanceof HSQLDialect) ) s.find("select foo.string from Foo foo where foo.foo.foo.foo = foo.foo.foo"); 361 s.find("select foo.string from Foo foo where foo.foo.foo = 'bar' and foo.foo.foo.foo = 'baz'"); 362 s.find("select foo.string from Foo foo where foo.foo.foo.foo.string = 'a' and foo.foo.string = 'b'"); 363 364 s.find("from Bar bar, foo in elements(bar.baz.fooArray)"); 365 366 368 if ( (getDialect() instanceof DB2Dialect) && !(getDialect() instanceof DerbyDialect) ) { 369 s.find("from Foo foo where lower( foo.foo.string ) = 'foo'"); 370 s.find("from Foo foo where lower( (foo.foo.string || 'foo') || 'bar' ) = 'foo'"); 371 s.find("from Foo foo where repeat( (foo.foo.string || 'foo') || 'bar', 2 ) = 'foo'"); 372 s.find("from Bar foo where foo.foo.integer is not null and repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'"); 373 s.find("from Bar foo where foo.foo.integer is not null or repeat( (foo.foo.string || 'foo') || 'bar', (5+5)/2 ) = 'foo'"); 374 } 375 if (getDialect() instanceof SybaseDialect) { 376 s.iterate("select baz from Baz as baz join baz.fooArray foo group by baz order by sum(foo.float)"); 377 } 378 379 s.find("from Foo as foo where foo.component.glarch.name is not null"); 380 s.find("from Foo as foo left outer join foo.component.glarch as glarch where glarch.name = 'foo'"); 381 382 list = s.find("from Foo"); 383 assertTrue( list.size()==2 && list.get(0) instanceof FooProxy ); 384 list = s.find("from Foo foo left outer join foo.foo"); 385 assertTrue( list.size()==2 && ( (Object []) list.get(0) )[0] instanceof FooProxy ); 386 387 s.createQuery("from Bar, Bar").list(); 388 s.createQuery("from Foo, Bar").list(); 389 s.find("from Baz baz left join baz.fooToGlarch, Bar bar join bar.foo"); 390 s.find("from Baz baz left join baz.fooToGlarch join baz.fooSet"); 391 s.find("from Baz baz left join baz.fooToGlarch join fetch baz.fooSet foo left join fetch foo.foo"); 392 393 list = s.find("from Foo foo where foo.string='osama bin laden' and foo.boolean = true order by foo.string asc, foo.component.count desc"); 394 assertTrue( "empty query", list.size()==0 ); 395 Iterator iter = s.iterate("from Foo foo where foo.string='osama bin laden' order by foo.string asc, foo.component.count desc"); 396 assertTrue( "empty iterator", !iter.hasNext() ); 397 398 list = s.find("select foo.foo from Foo foo"); 399 assertTrue( "query", list.size()==1 ); 400 assertTrue( "returned object", list.get(0)==foo.getFoo() ); 401 foo.getFoo().setFoo(foo); 402 foo.setString("fizard"); 403 if ( 405 !(getDialect() instanceof MySQLDialect) && 406 !(getDialect() instanceof HSQLDialect) && 407 !(getDialect() instanceof MckoiDialect) && 408 !(getDialect() instanceof SAPDBDialect) && 409 !(getDialect() instanceof PointbaseDialect) && 410 !(getDialect() instanceof DerbyDialect) 411 ) { 412 if ( !( getDialect() instanceof InterbaseDialect ) ) { 414 list = s.find("from Foo foo where ? = some elements(foo.component.importantDates)", new Date (), Hibernate.DATE); 415 assertTrue( "component query", list.size()==2 ); 416 } 417 if( !( getDialect() instanceof TimesTenDialect)) { 418 list = s.find("from Foo foo where size(foo.component.importantDates) = 3"); assertTrue( "component query", list.size()==2 ); 420 list = s.find("from Foo foo where 0 = size(foo.component.importantDates)"); 421 assertTrue( "component query", list.size()==0 ); 422 } 423 list = s.find("from Foo foo where exists elements(foo.component.importantDates)"); 424 assertTrue( "component query", list.size()==2 ); 425 s.find("from Foo foo where not exists (from Bar bar where bar.id = foo.id)"); 426 427 s.find("select foo.foo from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)"); 428 s.find("select foo.foo from Foo foo where foo = some(from Foo x where (x.long > foo.foo.long))"); 429 if ( !( getDialect() instanceof TimesTenDialect)) { 430 s.find("select foo.foo from Foo foo where foo.long = some( select max(x.long) from Foo x where (x.long > foo.foo.long) group by x.foo )"); 431 } 432 s.find("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long) and foo.foo.string='baz'"); 433 s.find("from Foo foo where foo.foo.string='baz' and foo = some(select x from Foo x where x.long > foo.foo.long)"); 434 s.find("from Foo foo where foo = some(select x from Foo x where x.long > foo.foo.long)"); 435 436 s.iterate("select foo.string, foo.date, foo.foo.string, foo.id from Foo foo, Baz baz where foo in elements(baz.fooArray) and foo.string like 'foo'"); 437 } 438 list = s.find("from Foo foo where foo.component.count is null order by foo.component.count"); 439 assertTrue( "component query", list.size()==0 ); 440 list = s.find("from Foo foo where foo.component.name='foo'"); 441 assertTrue( "component query", list.size()==2 ); 442 list = s.find("select distinct foo.component.name, foo.component.name from Foo foo where foo.component.name='foo'"); 443 assertTrue( "component query", list.size()==1 ); 444 list = s.find("select distinct foo.component.name, foo.id from Foo foo where foo.component.name='foo'"); 445 assertTrue( "component query", list.size()==2 ); 446 list = s.find("select foo.foo from Foo foo"); 447 assertTrue( "query", list.size()==2 ); 448 list = s.find("from Foo foo where foo.id=?", foo.getKey(), Hibernate.STRING); 449 assertTrue( "id query", list.size()==1 ); 450 list = s.find("from Foo foo where foo.key=?", foo.getKey(), Hibernate.STRING); 451 assertTrue( "named id query", list.size()==1 ); 452 assertTrue( "id query", list.get(0)==foo ); 453 list = s.find("select foo.foo from Foo foo where foo.string='fizard'"); 454 assertTrue( "query", list.size()==1 ); 455 assertTrue( "returned object", list.get(0)==foo.getFoo() ); 456 list = s.find("from Foo foo where foo.component.subcomponent.name='bar'"); 457 assertTrue( "components of components", list.size()==2 ); 458 list = s.find("select foo.foo from Foo foo where foo.foo.id=?", foo.getFoo().getKey(), Hibernate.STRING); 459 assertTrue( "by id query", list.size()==1 ); 460 assertTrue( "by id returned object", list.get(0)==foo.getFoo() ); 461 462 s.find( "from Foo foo where foo.foo = ?", foo.getFoo(), Hibernate.entity(Foo.class) ); 463 464 assertTrue( !s.iterate("from Bar bar where bar.string='a string' or bar.string='a string'").hasNext() ); 465 466 iter = s.iterate( 467 "select foo.component.name, elements(foo.component.importantDates) from Foo foo where foo.foo.id=?", 468 foo.getFoo().getKey(), 469 Hibernate.STRING 470 ); 471 int i=0; 472 while ( iter.hasNext() ) { 473 i++; 474 Object [] row = (Object []) iter.next(); 475 assertTrue( row[0] instanceof String && ( row[1]==null || row[1] instanceof Date ) ); 476 } 477 assertTrue(i==3); iter = s.iterate( 479 "select max( elements(foo.component.importantDates) ) from Foo foo group by foo.id" 480 ); 481 assertTrue( iter.next() instanceof Date ); 482 483 list = s.find( 484 "select foo.foo.foo.foo from Foo foo, Foo foo2 where" 485 + " foo = foo2.foo and not not ( not foo.string='fizard' )" 486 + " and foo2.string between 'a' and (foo.foo.string)" 487 + ( ( getDialect() instanceof HSQLDialect || getDialect() instanceof InterbaseDialect || getDialect() instanceof TimesTenDialect)? 488 " and ( foo2.string in ( 'fiz', 'blah') or 1=1 )" 489 : 490 " and ( foo2.string in ( 'fiz', 'blah', foo.foo.string, foo.string, foo2.string ) )" 491 ) 492 ); 493 assertTrue( "complex query", list.size()==1 ); 494 assertTrue( "returned object", list.get(0)==foo ); 495 foo.setString("from BoogieDown -tinsel town =!@#$^&*())"); 496 list = s.find("from Foo foo where foo.string='from BoogieDown -tinsel town =!@#$^&*())'"); 497 assertTrue( "single quotes", list.size()==1 ); 498 list = s.find("from Foo foo where not foo.string='foo''bar'"); 499 assertTrue( "single quotes", list.size()==2 ); 500 list = s.find("from Foo foo where foo.component.glarch.next is null"); 501 assertTrue( "query association in component", list.size()==2 ); 502 Bar bar = new Bar(); 503 Baz baz = new Baz(); 504 baz.setDefaults(); 505 bar.setBaz(baz); 506 baz.setManyToAny( new ArrayList () ); 507 baz.getManyToAny().add(bar); 508 baz.getManyToAny().add(foo); 509 s.save(bar); 510 s.save(baz); 511 list = s.find(" from Bar bar where bar.baz.count=667 and bar.baz.count!=123 and not bar.baz.name='1-E-1'"); 512 assertTrue( "query many-to-one", list.size()==1 ); 513 list = s.find(" from Bar i where i.baz.name='Bazza'"); 514 assertTrue( "query many-to-one", list.size()==1 ); 515 516 Iterator rs = s.iterate("select count(distinct foo.foo) from Foo foo"); 517 assertTrue( "count", ( (Integer ) rs.next() ).intValue()==2 ); 518 assertTrue( !rs.hasNext() ); 519 rs = s.iterate("select count(foo.foo.boolean) from Foo foo"); 520 assertTrue( "count", ( (Integer ) rs.next() ).intValue()==2 ); 521 assertTrue( !rs.hasNext() ); 522 rs = s.iterate("select count(*), foo.int from Foo foo group by foo.int"); 523 assertTrue( "count(*) group by", ( (Object []) rs.next() )[0].equals( new Integer (3) ) ); 524 assertTrue( !rs.hasNext() ); 525 rs = s.iterate("select sum(foo.foo.int) from Foo foo"); 526 assertTrue( "sum", ( (Integer ) rs.next() ).intValue()==4 ); 527 assertTrue( !rs.hasNext() ); 528 rs = s.iterate("select count(foo) from Foo foo where foo.id=?", foo.getKey(), Hibernate.STRING); 529 assertTrue( "id query count", ( (Integer ) rs.next() ).intValue()==1 ); 530 assertTrue( !rs.hasNext() ); 531 532 list = s.find( "from Foo foo where foo.boolean = ?", new Boolean (true), Hibernate.BOOLEAN ); 533 534 list = s.find("select new Foo(fo.x) from Fo fo"); 535 list = s.find("select new Foo(fo.integer) from Foo fo"); 536 537 list = s.createQuery("select new Foo(fo.x) from Foo fo") 538 .setCacheable(true) 540 .list(); 541 assertTrue(list.size()==3); 542 list = s.createQuery("select new Foo(fo.x) from Foo fo") 543 .setCacheable(true) 545 .list(); 546 assertTrue(list.size()==3); 547 548 rs = s.iterate("select new Foo(fo.x) from Foo fo"); 549 assertTrue( "projection iterate (results)", rs.hasNext() ); 550 assertTrue( "projection iterate (return check)", Foo.class.isAssignableFrom( rs.next().getClass() ) ); 551 552 ScrollableResults sr = s.createQuery("select new Foo(fo.x) from Foo fo").scroll(); 553 assertTrue( "projection scroll (results)", sr.next() ); 554 assertTrue( "projection scroll (return check)", Foo.class.isAssignableFrom( sr.get(0).getClass() ) ); 555 556 list = s.find("select foo.long, foo.component.name, foo, foo.foo from Foo foo"); 557 rs = list.iterator(); 558 int count=0; 559 while ( rs.hasNext() ) { 560 count++; 561 Object [] row = (Object []) rs.next(); 562 assertTrue( row[0] instanceof Long ); 563 assertTrue( row[1] instanceof String ); 564 assertTrue( row[2] instanceof Foo ); 565 assertTrue( row[3] instanceof Foo ); 566 } 567 assertTrue(count!=0); 568 list = s.find("select avg(foo.float), max(foo.component.name), count(distinct foo.id) from Foo foo"); 569 rs = list.iterator(); 570 count=0; 571 while ( rs.hasNext() ) { 572 count++; 573 Object [] row = (Object []) rs.next(); 574 assertTrue( row[0] instanceof Float ); 575 assertTrue( row[1] instanceof String ); 576 assertTrue( row[2] instanceof Integer ); 577 } 578 assertTrue(count!=0); 579 list = s.find("select foo.long, foo.component, foo, foo.foo from Foo foo"); 580 rs = list.iterator(); 581 count=0; 582 while ( rs.hasNext() ) { 583 count++; 584 Object [] row = (Object []) rs.next(); 585 assertTrue( row[0] instanceof Long ); 586 assertTrue( row[1] instanceof FooComponent ); 587 assertTrue( row[2] instanceof Foo ); 588 assertTrue( row[3] instanceof Foo ); 589 } 590 assertTrue(count!=0); 591 592 s.save( new Holder("ice T") ); 593 s.save( new Holder("ice cube") ); 594 595 assertTrue( s.find("from java.lang.Object as o").size()==15 ); 596 assertTrue( s.find("from Named").size()==7 ); 597 assertTrue( s.find("from Named n where n.name is not null").size()==4 ); 598 iter = s.iterate("from Named n"); 599 while ( iter.hasNext() ) { 600 assertTrue( iter.next() instanceof Named ); 601 } 602 603 s.save( new Holder("bar") ); 604 iter = s.iterate("from Named n0, Named n1 where n0.name = n1.name"); 605 int cnt = 0; 606 while ( iter.hasNext() ) { 607 Object [] row = (Object []) iter.next(); 608 if ( row[0]!=row[1] ) cnt++; 609 } 610 if ( !(getDialect() instanceof HSQLDialect) ) { 611 assertTrue(cnt==2); 612 assertTrue( s.find("from Named n0, Named n1 where n0.name = n1.name").size()==7 ); 613 } 614 615 Query qu = s.createQuery("from Named n where n.name = :name"); 616 qu.getReturnTypes(); 617 qu.getNamedParameters(); 618 619 iter = s.iterate("from java.lang.Object"); 620 int c = 0; 621 while ( iter.hasNext() ) { 622 iter.next(); 623 c++; 624 } 625 assertTrue(c==16); 626 627 s.iterate("select baz.code, min(baz.count) from Baz baz group by baz.code"); 628 629 iter = s.iterate("selecT baz from Baz baz where baz.stringDateMap['foo'] is not null or baz.stringDateMap['bar'] = ?", new Date (), Hibernate.DATE); 630 assertFalse( iter.hasNext() ); 631 list = s.find("select baz from Baz baz where baz.stringDateMap['now'] is not null"); 632 assertTrue( list.size()==1 ); 633 list = s.find("select baz from Baz baz where baz.stringDateMap['now'] is not null and baz.stringDateMap['big bang'] < baz.stringDateMap['now']"); 634 assertTrue( list.size()==1 ); 635 list = s.find("select index(date) from Baz baz join baz.stringDateMap date"); 636 System.out.println(list); 637 assertTrue( list.size()==2 ); 638 639 s.find("from Foo foo where foo.integer not between 1 and 5 and foo.string not in ('cde', 'abc') and foo.string is not null and foo.integer<=3"); 640 641 s.find("from Baz baz inner join baz.collectionComponent.nested.foos foo where foo.string is null"); 642 if ( !(getDialect() instanceof MySQLDialect) && !(getDialect() instanceof MckoiDialect) && !(getDialect() instanceof SAPDBDialect) && !(getDialect() instanceof PointbaseDialect) ) { 643 s.find("from Baz baz inner join baz.fooSet where '1' in (from baz.fooSet foo where foo.string is not null)"); 644 s.find("from Baz baz where 'a' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)"); 645 s.find("from Baz baz where 'b' in elements(baz.collectionComponent.nested.foos) and 1.0 in elements(baz.collectionComponent.nested.floats)"); 646 } 647 648 s.find("from Foo foo join foo.foo where foo.foo in ('1','2','3')"); 649 if ( !(getDialect() instanceof HSQLDialect) ) s.find("from Foo foo left join foo.foo where foo.foo in ('1','2','3')"); 650 s.find("select foo.foo from Foo foo where foo.foo in ('1','2','3')"); 651 s.find("select foo.foo.string from Foo foo where foo.foo in ('1','2','3')"); 652 s.find("select foo.foo.string from Foo foo where foo.foo.string in ('1','2','3')"); 653 s.find("select foo.foo.long from Foo foo where foo.foo.string in ('1','2','3')"); 654 s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') or foo.foo.long in (1,2,3)"); 655 s.find("select count(*) from Foo foo where foo.foo.string in ('1','2','3') group by foo.foo.long"); 656 657 s.find("from Foo foo1 left join foo1.foo foo2 left join foo2.foo where foo1.string is not null"); 658 s.find("from Foo foo1 left join foo1.foo.foo where foo1.string is not null"); 659 s.find("from Foo foo1 left join foo1.foo foo2 left join foo1.foo.foo foo3 where foo1.string is not null"); 660 661 s.find("select foo.formula from Foo foo where foo.formula > 0"); 662 663 int len = s.find("from Foo as foo join foo.foo as foo2 where foo2.id >'a' or foo2.id <'a'").size(); 664 assertTrue(len==2); 665 666 s.delete("from Holder"); 667 s.flush(); 668 s.connection().commit(); 669 s.close(); 670 671 s = openSession(); 672 baz = (Baz) s.createQuery("from Baz baz left outer join fetch baz.manyToAny").uniqueResult(); 673 assertTrue( Hibernate.isInitialized( baz.getManyToAny() ) ); 674 assertTrue( baz.getManyToAny().size()==2 ); 675 BarProxy barp = (BarProxy) baz.getManyToAny().get(0); 676 s.find("from Baz baz join baz.manyToAny"); 677 assertTrue( s.find("select baz from Baz baz join baz.manyToAny a where index(a) = 0").size()==1 ); 678 679 FooProxy foop = (FooProxy) s.get( Foo.class, foo.getKey() ); 680 assertTrue( foop == baz.getManyToAny().get(1) ); 681 682 barp.setBaz(baz); 683 assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['now'] is not null").size()==1 ); 684 assertTrue( s.find("select bar from Bar bar join bar.baz b where b.stringDateMap['big bang'] < b.stringDateMap['now'] and b.stringDateMap['now'] is not null").size()==1 ); 685 assertTrue( s.find("select bar from Bar bar where bar.baz.stringDateMap['big bang'] < bar.baz.stringDateMap['now'] and bar.baz.stringDateMap['now'] is not null").size()==1 ); 686 687 list = s.find("select foo.string, foo.component, foo.id from Bar foo"); 688 assertTrue ( ( (FooComponent) ( (Object []) list.get(0) )[1] ).getName().equals("foo") ); 689 list = s.find("select elements(baz.components) from Baz baz"); 690 assertTrue( list.size()==2 ); 691 list = s.find("select bc.name from Baz baz join baz.components bc"); 692 assertTrue( list.size()==2 ); 693 695 s.createQuery("from Foo foo where foo.integer < 10 order by foo.string").setMaxResults(12).list(); 696 697 s.delete(barp); 698 s.delete(baz); 699 s.delete( foop.getFoo() ); 700 s.delete(foop); 701 s.flush(); 702 s.connection().commit(); 703 s.close(); 704 }
|