1 package org.hibernate.test.hql; 3 4 import java.io.PrintWriter ; 5 import java.io.StringWriter ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 10 import junit.framework.Test; 11 import junit.framework.TestSuite; 12 13 import org.hibernate.dialect.HSQLDialect; 14 import org.hibernate.dialect.MySQLDialect; 15 import org.hibernate.dialect.Oracle9Dialect; 16 import org.hibernate.dialect.function.SQLFunction; 17 import org.hibernate.hql.ast.DetailedSemanticException; 18 import org.hibernate.hql.ast.QuerySyntaxException; 19 import org.hibernate.hql.ast.tree.ConstructorNode; 20 import org.hibernate.hql.ast.tree.DotNode; 21 import org.hibernate.hql.ast.tree.IndexNode; 22 import org.hibernate.hql.ast.tree.SelectClause; 23 24 import antlr.RecognitionException; 25 26 31 public class HQLTest extends QueryTranslatorTestCase { 32 33 public HQLTest(String x) { 34 super( x ); 35 SelectClause.VERSION2_SQL = true; 36 } 37 38 protected boolean dropAfterFailure() { 39 return false; 40 } 41 42 44 public void testCollectionOrderBy() { 45 assertTranslation("from Animal a join a.offspring o order by a.description"); 46 assertTranslation("from Animal a join fetch a.offspring order by a.description"); 47 assertTranslation("from Animal a join a.offspring o order by a.description, o.description"); 48 } 49 50 public void testExpressionWithParamInFunction() { 51 assertTranslation("from Animal a where abs(a.bodyWeight-:param) < 2.0"); 52 assertTranslation("from Animal a where abs(:param - a.bodyWeight) < 2.0"); 53 assertTranslation("from Animal where abs(:x - :y) < 2.0"); 54 assertTranslation("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0"); 55 assertTranslation("from Animal where lower(upper(:foo)) like 'f%'"); 56 assertTranslation("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0"); 57 if ( getDialect() instanceof MySQLDialect ) return; 58 assertTranslation("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'"); 59 } 60 61 public void testCompositeKeysWithPropertyNamedId() { 62 assertTranslation( "select e.id.id from EntityWithCrazyCompositeKey e" ); 63 assertTranslation( "select max(e.id.id) from EntityWithCrazyCompositeKey e" ); 64 } 65 66 public void testMaxindexHqlFunctionInElementAccessor() { 67 assertTranslation( "select c from ContainerX c where c.manyToMany[ maxindex(c.manyToMany) ].count = 2" ); 69 assertTranslation( "select c from Container c where c.manyToMany[ maxIndex(c.manyToMany) ].count = 2" ); 70 } 71 72 public void testMultipleElementAccessorOperators() throws Exception { 73 assertTranslation( "select c from ContainerX c where c.oneToMany[ c.manyToMany[0].count ].name = 's'" ); 75 assertTranslation( "select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'" ); 76 } 77 78 82 83 public void testKeyManyToOneJoin() { 84 assertTranslation( "from Order o left join fetch o.lineItems li left join fetch li.product p" ); 86 assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" ); 87 assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" ); 88 } 89 90 public void testDuplicateExplicitJoin() throws Exception { 91 assertTranslation( "from Animal a join a.mother m1 join a.mother m2" ); 93 assertTranslation( "from Zoo zoo join zoo.animals an join zoo.mammals m" ); 94 assertTranslation( "from Zoo zoo join zoo.mammals an join zoo.mammals m" ); 95 } 96 97 99 public void testIndexWithExplicitJoin() throws Exception { 100 assertTranslation( "from Zoo zoo join zoo.animals an where zoo.mammals[ index(an) ] = an" ); 102 assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals[ index(dog) ] = dog" ); 103 assertTranslation( "from Zoo zoo join zoo.mammals dog where dog = zoo.mammals[ index(dog) ]" ); 104 } 105 106 public void testOneToManyMapIndex() throws Exception { 107 assertTranslation( "from Zoo zoo where zoo.mammals['dog'].description like '%black%'" ); 109 assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.description like '%black%'" ); 110 assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.id = 1234" ); 111 assertTranslation( "from Zoo zoo where zoo.animals['1234'].description like '%black%'" ); 112 } 113 114 public void testExplicitJoinMapIndex() throws Exception { 115 assertTranslation( "from Zoo zoo, Dog dog where zoo.mammals['dog'] = dog" ); 117 assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals['dog'] = dog" ); 118 } 119 120 public void testIndexFunction() throws Exception { 121 assertTranslation( "from Zoo zoo join zoo.mammals dog where index(dog) = 'dog'" ); 125 assertTranslation( "from Zoo zoo join zoo.animals an where index(an) = '1234'" ); 126 } 127 128 public void testSelectCollectionOfValues() throws Exception { 129 assertTranslation( "select baz, date from Baz baz join baz.stringDateMap date where index(date) = 'foo'" ); 132 } 133 134 public void testCollectionOfValues() throws Exception { 135 assertTranslation( "from Baz baz join baz.stringDateMap date where index(date) = 'foo'" ); 138 } 139 140 public void testHHH719() throws Exception { 141 assertTranslation("from Baz b order by org.bazco.SpecialFunction(b.id)"); 142 } 143 144 146 public void testExplicitJoinsInSubquery() { 147 assertTranslation( 151 "from org.hibernate.test.hql.Animal as animal " + 152 "where animal.id in (" + 153 " select a.id " + 154 " from org.hibernate.test.hql.Animal as a " + 155 " left join a.mother as mo" + 156 ")" 157 ); 158 } 159 160 public void testImplicitJoinsInGroupBy() { 161 assertTranslation( 162 "select o.mother.bodyWeight, count(distinct o) " + 163 "from Animal an " + 164 " join an.offspring as o " + 165 "group by o.mother.bodyWeight" 166 ); 167 } 168 169 public void testCrazyIdFieldNames() { 170 DotNode.useThetaStyleImplicitJoins = true; 171 175 String hql = "select e.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null"; 176 assertTranslation( hql, new HashMap (), false, ( String ) null ); 177 178 hql = "select e.heresAnotherCrazyIdFieldName.heresAnotherCrazyIdFieldName from MoreCrazyIdFieldNameStuffEntity e where e.heresAnotherCrazyIdFieldName is not null"; 179 assertTranslation( hql, new HashMap (), false, ( String ) null ); 180 181 DotNode.useThetaStyleImplicitJoins = false; 182 } 183 184 public void testSizeFunctionAndProperty() { 185 assertTranslation("from Animal a where a.offspring.size > 0"); 186 assertTranslation("from Animal a join a.offspring where a.offspring.size > 1"); 187 assertTranslation("from Animal a where size(a.offspring) > 0"); 188 assertTranslation("from Animal a join a.offspring o where size(a.offspring) > 1"); 189 assertTranslation("from Animal a where size(a.offspring) > 1 and size(a.offspring) < 100"); 190 191 assertTranslation("from Human a where a.family.size > 0"); 192 assertTranslation("from Human a join a.family where a.family.size > 1"); 193 assertTranslation("from Human a where size(a.family) > 0"); 194 assertTranslation("from Human a join a.family o where size(a.family) > 1"); 195 assertTranslation("from Human a where a.family.size > 0 and a.family.size < 100"); 196 } 197 198 public void testFromOnly() throws Exception { 200 assertTranslation( "from Animal" ); 202 assertTranslation( "from Model" ); 203 } 204 205 public void testJoinPathEndingInValueCollection() { 206 assertTranslation( "select h from Human as h join h.nickNames as nn where h.nickName=:nn1 and (nn=:nn2 or nn=:nn3)" ); 207 } 208 209 public void testSerialJoinPathEndingInValueCollection() { 210 assertTranslation( "select h from Human as h join h.friends as f join f.nickNames as nn where h.nickName=:nn1 and (nn=:nn2 or nn=:nn3)" ); 212 } 213 214 public void testImplicitJoinContainedByCollectionFunction() { 215 assertTranslation( "from Human as h where 'shipping' in indices(h.father.addresses)" ); 217 assertTranslation( "from Human as h where 'shipping' in indices(h.father.father.addresses)" ); 218 assertTranslation( "from Human as h where 'sparky' in elements(h.father.nickNames)" ); 219 assertTranslation( "from Human as h where 'sparky' in elements(h.father.father.nickNames)" ); 220 } 221 222 223 public void testImpliedJoinInSubselectFrom() { 224 assertTranslation( "from Animal a where exists( from a.mother.offspring )" ); 226 } 227 228 public void testSubselectImplicitJoins() { 229 assertTranslation( "from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count )" ); 231 } 232 233 234 public void testCollectionOfValuesSize() throws Exception { 235 assertTranslation( "select size(baz.stringDateMap) from org.hibernate.test.legacy.Baz baz" ); 237 } 238 239 public void testCollectionFunctions() throws Exception { 240 assertTranslation( "from Zoo zoo where size(zoo.animals) > 100" ); 242 assertTranslation( "from Zoo zoo where maxindex(zoo.mammals) = 'dog'" ); 243 } 244 245 public void testImplicitJoinInExplicitJoin() throws Exception { 246 assertTranslation( "from Animal an inner join an.mother.mother gm" ); 247 assertTranslation( "from Animal an inner join an.mother.mother.mother ggm" ); 248 assertTranslation( "from Animal an inner join an.mother.mother.mother.mother gggm" ); 249 } 250 251 public void testImpliedManyToManyProperty() throws Exception { 252 assertTranslation( "select c from ContainerX c where c.manyToMany[0].name = 's'" ); 254 } 255 256 public void testCollectionSize() throws Exception { 257 assertTranslation( "select size(zoo.animals) from Zoo zoo" ); 259 } 260 261 272 273 public void testFetchCollectionOfValues() throws Exception { 274 assertTranslation( "from Baz baz left join fetch baz.stringSet" ); 275 } 276 277 public void testFetchList() throws Exception { 278 assertTranslation( "from User u join fetch u.permissions" ); 279 } 280 281 public void testCollectionFetchWithExplicitThetaJoin() { 282 assertTranslation( "select m from Master m1, Master m left join fetch m.details where m.name=m1.name" ); 283 } 284 285 290 291 public void testListElementFunctionInWhere() throws Exception { 292 assertTranslation( "from User u where 'read' in elements(u.permissions)" ); 293 assertTranslation( "from User u where 'write' <> all elements(u.permissions)" ); 294 } 295 296 300 301 public void testManyToManyMaxElementFunctionInWhere() throws Exception { 302 assertTranslation( "from Human human where 5 = maxelement(human.friends)" ); 304 } 305 306 public void testCollectionIndexFunctionsInWhere() throws Exception { 307 assertTranslation( "from Zoo zoo where 4 = maxindex(zoo.animals)" ); 308 assertTranslation( "from Zoo zoo where 2 = minindex(zoo.animals)" ); 309 } 310 311 public void testCollectionIndicesInWhere() throws Exception { 312 assertTranslation( "from Zoo zoo where 4 > some indices(zoo.animals)" ); 313 assertTranslation( "from Zoo zoo where 4 > all indices(zoo.animals)" ); 314 } 315 316 public void testIndicesInWhere() throws Exception { 317 assertTranslation( "from Zoo zoo where 4 in indices(zoo.animals)" ); 318 assertTranslation( "from Zoo zoo where exists indices(zoo.animals)" ); 319 } 320 321 public void testCollectionElementInWhere() throws Exception { 322 assertTranslation( "from Zoo zoo where 4 > some elements(zoo.animals)" ); 323 assertTranslation( "from Zoo zoo where 4 > all elements(zoo.animals)" ); 324 } 325 326 public void testElementsInWhere() throws Exception { 327 assertTranslation( "from Zoo zoo where 4 in elements(zoo.animals)" ); 328 assertTranslation( "from Zoo zoo where exists elements(zoo.animals)" ); 329 } 330 331 public void testNull() throws Exception { 332 assertTranslation( "from Human h where h.nickName is null" ); 333 assertTranslation( "from Human h where h.nickName is not null" ); 334 } 335 336 public void testSubstitutions() throws Exception { 337 assertTranslation( "from Human h where h.pregnant = true" ); 338 assertTranslation( "from Human h where h.pregnant = foo" ); 339 } 340 341 public void testWhere() throws Exception { 342 assertTranslation( "from Animal an where an.bodyWeight > 10" ); 343 assertTranslation( "from Animal an where not an.bodyWeight > 10" ); 345 assertTranslation( "from Animal an where an.bodyWeight between 0 and 10" ); 346 assertTranslation( "from Animal an where an.bodyWeight not between 0 and 10" ); 347 assertTranslation( "from Animal an where sqrt(an.bodyWeight)/2 > 10" ); 348 assertTranslation( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" ); 350 } 351 352 public void testEscapedQuote() throws Exception { 353 assertTranslation( "from Human h where h.nickName='1 ov''tha''few'"); 354 } 355 356 public void testCaseWhenElse() { 357 assertTranslation( "from Human h where case when h.nickName='1ovthafew' then 'Gavin' when h.nickName='turin' then 'Christian' else h.nickName end = h.name.first" ); 358 } 359 360 public void testCaseExprWhenElse() { 361 assertTranslation( "from Human h where case h.nickName when '1ovthafew' then 'Gavin' when 'turin' then 'Christian' else h.nickName end = h.name.first" ); 362 } 363 364 public void testInvalidHql() throws Exception { 365 Exception newException = compileBadHql( "from Animal foo where an.bodyWeight > 10", false ); 366 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 367 newException = compileBadHql( "select an.name from Animal foo", false ); 368 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 369 newException = compileBadHql( "from Animal foo where an.verybogus > 10", false ); 370 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 371 newException = compileBadHql( "select an.boguspropertyname from Animal foo", false ); 372 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 373 newException = compileBadHql( "select an.name", false ); 374 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 375 newException = compileBadHql( "from Animal an where (((an.bodyWeight > 10 and an.bodyWeight < 100)) or an.bodyWeight is null", false ); 376 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 377 newException = compileBadHql( "from Animal an where an.bodyWeight is null where an.bodyWeight is null", false ); 378 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 379 newException = compileBadHql( "from where name='foo'", false ); 380 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 381 newException = compileBadHql( "from NonexistentClass where name='foo'", false ); 382 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 383 newException = compileBadHql( "select new FOO_BOGUS_Animal(an.description, an.bodyWeight) from Animal an", false ); 384 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 385 newException = compileBadHql( "select new Animal(an.description, an.bodyWeight, 666) from Animal an", false ); 386 assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); 387 388 } 389 390 public void testWhereBetween() throws Exception { 391 assertTranslation( "from Animal an where an.bodyWeight between 1 and 10" ); 393 } 394 395 public void testConcatenation() { 396 if ( getDialect() instanceof MySQLDialect ) return; assertTranslation("from Human h where h.nickName = '1' || 'ov' || 'tha' || 'few'"); 398 } 399 400 public void testWhereLike() throws Exception { 401 assertTranslation( "from Animal a where a.description like '%black%'" ); 402 assertTranslation( "from Animal an where an.description like '%fat%'" ); 403 assertTranslation( "from Animal an where lower(an.description) like '%fat%'" ); 404 } 405 406 public void testWhereIn() throws Exception { 407 assertTranslation( "from Animal an where an.description in ('fat', 'skinny')" ); 408 } 409 410 public void testLiteralInFunction() throws Exception { 411 assertTranslation( "from Animal an where an.bodyWeight > abs(5)" ); 412 assertTranslation( "from Animal an where an.bodyWeight > abs(-5)" ); 413 } 414 415 public void testExpressionInFunction() throws Exception { 416 assertTranslation( "from Animal an where an.bodyWeight > abs(3-5)" ); 417 assertTranslation( "from Animal an where an.bodyWeight > abs(3/5)" ); 418 assertTranslation( "from Animal an where an.bodyWeight > abs(3+5)" ); 419 assertTranslation( "from Animal an where an.bodyWeight > abs(3*5)" ); 420 SQLFunction concat = (SQLFunction) getDialect().getFunctions().get("concat"); 421 List list = new ArrayList (); list.add("'fat'"); list.add("'skinny'"); 422 assertTranslation( "from Animal an where an.description = " + concat.render(list, getSessionFactoryImplementor()) ); 423 } 424 425 public void testNotOrWhereClause() { 426 assertTranslation( "from Simple s where 'foo'='bar' or not 'foo'='foo'" ); 427 assertTranslation( "from Simple s where 'foo'='bar' or not ('foo'='foo')" ); 428 assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' )" ); 429 assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' )" ); 430 assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' ) or not ('x'='y')" ); 431 assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' ) and not ('x'='y')" ); 432 assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' ) and 'x'='y'" ); 433 assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' ) or 'x'='y'" ); 434 assertTranslation( "from Simple s where 'foo'='bar' and 'foo'='foo' or not 'x'='y'" ); 435 assertTranslation( "from Simple s where 'foo'='bar' or 'foo'='foo' and not 'x'='y'" ); 436 assertTranslation( "from Simple s where ('foo'='bar' and 'foo'='foo') or 'x'='y'" ); 437 assertTranslation( "from Simple s where ('foo'='bar' or 'foo'='foo') and 'x'='y'" ); 438 assertTranslation( "from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )" ); 439 } 440 441 public void testComplexExpressionInFunction() throws Exception { 442 assertTranslation( "from Animal an where an.bodyWeight > abs((3-5)/4)" ); 443 } 444 445 public void testStandardFunctions() throws Exception { 446 assertTranslation( "from Animal where current_date = current_time" ); 447 assertTranslation( "from Animal a where upper(a.description) = 'FAT'" ); 448 assertTranslation( "select lower(a.description) from Animal a" ); 449 } 450 451 public void testOrderBy() throws Exception { 452 assertTranslation( "from Animal an order by an.bodyWeight" ); 453 assertTranslation( "from Animal an order by an.bodyWeight asc" ); 454 assertTranslation( "from Animal an order by an.bodyWeight desc" ); 455 assertTranslation( "from Human h order by sqrt(h.bodyWeight), year(h.birthdate)" ); 456 assertTranslation( "from Animal an order by sqrt(an.bodyWeight*4)/2" ); 457 assertTranslation( "from Animal an order by an.mother.bodyWeight" ); 458 assertTranslation( "from Animal an order by an.bodyWeight, an.description" ); 459 assertTranslation( "from Animal an order by an.bodyWeight asc, an.description desc" ); 460 } 461 462 public void testGroupByFunction() { 463 if ( getDialect() instanceof Oracle9Dialect ) return; 464 assertTranslation( "select count(*) from Human h group by year(h.birthdate)" ); 465 assertTranslation( "select count(*) from Human h group by trunc( sqrt(h.bodyWeight*4)/2 )" ); 466 assertTranslation( "select count(*) from Human h group by year(sysdate)" ); 467 } 468 469 public void testPolymorphism() throws Exception { 470 HashMap replacements = new HashMap (); 471 replacements.put( "false", "0" ); 472 replacements.put( "true", "1" ); 473 assertTranslation( "from Mammal" ); 474 assertTranslation( "from Dog" ); 475 assertTranslation( "from Mammal m where m.pregnant = false and m.bodyWeight > 10", replacements ); 476 assertTranslation( "from Dog d where d.pregnant = false and d.bodyWeight > 10", replacements ); 477 } 478 479 public void testTokenReplacement() throws Exception { 480 HashMap replacements = new HashMap (); 481 replacements.put( "false", "0" ); 482 replacements.put( "true", "1" ); 483 assertTranslation( "from Mammal m where m.pregnant = false and m.bodyWeight > 10", 484 replacements ); 485 } 486 487 public void testProduct() throws Exception { 488 HashMap replacements = new HashMap (); 489 replacements.put( "false", "0" ); 490 replacements.put( "true", "1" ); 491 assertTranslation( "from Animal, Animal" ); 492 assertTranslation( "from Animal x, Animal y where x.bodyWeight = y.bodyWeight" ); 493 assertTranslation( "from Animal x, Mammal y where x.bodyWeight = y.bodyWeight and not y.pregnant = true", replacements ); 494 assertTranslation( "from Mammal, Mammal" ); 495 } 496 497 public void testJoinedSubclassProduct() throws Exception { 498 assertTranslation( "from PettingZoo, PettingZoo" ); } 500 501 public void testProjectProduct() throws Exception { 502 assertTranslation( "select x from Human x, Human y where x.nickName = y.nickName" ); 503 assertTranslation( "select x, y from Human x, Human y where x.nickName = y.nickName" ); 504 } 505 506 public void testExplicitEntityJoins() throws Exception { 507 assertTranslation( "from Animal an inner join an.mother mo" ); 508 assertTranslation( "from Animal an left outer join an.mother mo" ); 509 assertTranslation( "from Animal an left outer join fetch an.mother" ); 510 } 511 512 public void testMultipleExplicitEntityJoins() throws Exception { 513 assertTranslation( "from Animal an inner join an.mother mo inner join mo.mother gm" ); 514 assertTranslation( "from Animal an left outer join an.mother mo left outer join mo.mother gm" ); 515 assertTranslation( "from Animal an inner join an.mother m inner join an.father f" ); 516 assertTranslation( "from Animal an left join fetch an.mother m left join fetch an.father f" ); 517 } 518 519 public void testMultipleExplicitJoins() throws Exception { 520 assertTranslation( "from Animal an inner join an.mother mo inner join an.offspring os" ); 521 assertTranslation( "from Animal an left outer join an.mother mo left outer join an.offspring os" ); 522 } 523 524 public void testExplicitEntityJoinsWithRestriction() throws Exception { 525 assertTranslation( "from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); 526 } 527 528 public void testIdProperty() throws Exception { 529 assertTranslation( "from Animal a where a.mother.id = 12" ); 530 } 531 532 public void testSubclassAssociation() throws Exception { 533 assertTranslation( "from DomesticAnimal da join da.owner o where o.nickName = 'Gavin'" ); 534 assertTranslation( "from DomesticAnimal da left join fetch da.owner" ); 535 assertTranslation( "from Human h join h.pets p where p.pregnant = 1" ); 536 assertTranslation( "from Human h join h.pets p where p.bodyWeight > 100" ); 537 assertTranslation( "from Human h left join fetch h.pets" ); 538 } 539 540 public void testExplicitCollectionJoins() throws Exception { 541 assertTranslation( "from Animal an inner join an.offspring os" ); 542 assertTranslation( "from Animal an left outer join an.offspring os" ); 543 } 544 545 public void testExplicitOuterJoinFetch() throws Exception { 546 assertTranslation( "from Animal an left outer join fetch an.offspring" ); 547 } 548 549 public void testExplicitOuterJoinFetchWithSelect() throws Exception { 550 assertTranslation( "select an from Animal an left outer join fetch an.offspring" ); 551 } 552 553 public void testExplicitJoins() throws Exception { 554 HashMap replacements = new HashMap (); 555 replacements.put( "false", "0" ); 556 replacements.put( "true", "1" ); 557 assertTranslation( "from Zoo zoo join zoo.mammals mam where mam.pregnant = true and mam.description like '%white%'", replacements ); 558 assertTranslation( "from Zoo zoo join zoo.animals an where an.description like '%white%'" ); 559 } 560 561 564 public void testMultibyteCharacterConstant() throws Exception { 565 assertTranslation( "from Zoo zoo join zoo.animals an where an.description like '%\u4e2d%'" ); 566 } 567 568 public void testImplicitJoins() throws Exception { 569 assertTranslation( "from Animal an where an.mother.bodyWeight > ?" ); 571 assertTranslation( "from Animal an where an.mother.bodyWeight > 10" ); 572 assertTranslation( "from Dog dog where dog.mother.bodyWeight > 10" ); 573 assertTranslation( "from Animal an where an.mother.mother.bodyWeight > 10" ); 575 assertTranslation( "from Animal an where an.mother is not null" ); 579 assertTranslation( "from Animal an where an.mother.id = 123" ); 581 } 582 583 public void testImplicitJoinInSelect() { 584 assertTranslation( "select foo, foo.long from Foo foo" ); 585 DotNode.useThetaStyleImplicitJoins = true; 586 assertTranslation( "select foo.foo from Foo foo" ); 587 assertTranslation( "select foo, foo.foo from Foo foo" ); 588 assertTranslation( "select foo.foo from Foo foo where foo.foo is not null" ); 589 DotNode.useThetaStyleImplicitJoins = false; 590 } 591 592 public void testSelectExpressions() { 593 DotNode.useThetaStyleImplicitJoins = true; 594 assertTranslation( "select an.mother.mother from Animal an" ); 595 assertTranslation( "select an.mother.mother.mother from Animal an" ); 596 assertTranslation( "select an.mother.mother.bodyWeight from Animal an" ); 597 assertTranslation( "select an.mother.zoo.id from Animal an" ); 598 assertTranslation( "select user.human.zoo.id from User user" ); 599 assertTranslation( "select u.userName, u.human.name.first from User u" ); 600 assertTranslation( "select u.human.name.last, u.human.name.first from User u" ); 601 assertTranslation( "select bar.baz.name from Bar bar" ); 602 assertTranslation( "select bar.baz.name, bar.baz.count from Bar bar" ); 603 DotNode.useThetaStyleImplicitJoins = false; 604 } 605 606 public void testSelectStandardFunctionsNoParens() throws Exception { 607 assertTranslation( "select current_date, current_time, current_timestamp from Animal" ); 608 } 609 610 public void testMapIndex() throws Exception { 611 assertTranslation( "from User u where u.permissions['hibernate']='read'" ); 612 } 613 614 623 624 public void testNamedParameters() throws Exception { 625 assertTranslation( "from Animal an where an.mother.bodyWeight > :weight" ); 626 } 627 628 630 public void testClassProperty() throws Exception { 631 assertTranslation( "from Animal a where a.mother.class = Reptile" ); 632 } 633 634 public void testComponent() throws Exception { 635 assertTranslation( "from Human h where h.name.first = 'Gavin'" ); 636 } 637 638 public void testSelectEntity() throws Exception { 639 assertTranslation( "select an from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); 640 assertTranslation( "select mo, an from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); 641 } 642 643 public void testValueAggregate() { 644 assertTranslation( "select max(p), min(p) from User u join u.permissions p" ); 645 } 646 647 public void testAggregation() throws Exception { 648 assertTranslation( "select count(an) from Animal an" ); 649 assertTranslation( "select count(*) from Animal an" ); 650 assertTranslation( "select count(distinct an) from Animal an" ); 651 assertTranslation( "select count(distinct an.id) from Animal an" ); 652 assertTranslation( "select count(all an.id) from Animal an" ); 653 } 654 655 public void testSelectProperty() throws Exception { 656 assertTranslation( "select an.bodyWeight, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); 657 } 658 659 public void testSelectEntityProperty() throws Exception { 660 DotNode.useThetaStyleImplicitJoins = true; 661 assertTranslation( "select an.mother from Animal an" ); 662 assertTranslation( "select an, an.mother from Animal an" ); 663 DotNode.useThetaStyleImplicitJoins = false; 664 } 665 666 public void testSelectDistinctAll() throws Exception { 667 assertTranslation( "select distinct an.description, an.bodyWeight from Animal an" ); 668 assertTranslation( "select all an from Animal an" ); 669 } 670 671 public void testSelectAssociatedEntityId() throws Exception { 672 assertTranslation( "select an.mother.id from Animal an" ); 673 } 674 675 public void testGroupBy() throws Exception { 676 assertTranslation( "select an.mother.id, max(an.bodyWeight) from Animal an group by an.mother.id" ); 677 assertTranslation( "select an.mother.id, max(an.bodyWeight) from Animal an group by an.mother.id having max(an.bodyWeight)>1.0" ); 678 } 679 680 public void testGroupByMultiple() throws Exception { 681 assertTranslation( "select s.id, s.count, count(t), max(t.date) from org.hibernate.test.legacy.Simple s, org.hibernate.test.legacy.Simple t where s.count = t.count group by s.id, s.count order by s.count" ); 682 } 683 684 public void testManyToMany() throws Exception { 685 assertTranslation( "from Human h join h.friends f where f.nickName = 'Gavin'" ); 686 assertTranslation( "from Human h join h.friends f where f.bodyWeight > 100" ); 687 } 688 689 public void testManyToManyElementFunctionInWhere() throws Exception { 690 assertTranslation( "from Human human where human in elements(human.friends)" ); 691 assertTranslation( "from Human human where human = some elements(human.friends)" ); 692 } 693 694 public void testManyToManyElementFunctionInWhere2() throws Exception { 695 assertTranslation( "from Human h1, Human h2 where h2 in elements(h1.family)" ); 696 assertTranslation( "from Human h1, Human h2 where 'father' in indices(h1.family)" ); 697 } 698 699 public void testManyToManyFetch() throws Exception { 700 assertTranslation( "from Human h left join fetch h.friends" ); 701 } 702 703 public void testManyToManyIndexAccessor() throws Exception { 704 assertTranslation( "select c from ContainerX c, Simple s where c.manyToMany[2] = s" ); 706 assertTranslation( "select s from ContainerX c, Simple s where c.manyToMany[2] = s" ); 707 assertTranslation( "from ContainerX c, Simple s where c.manyToMany[2] = s" ); 708 } 711 712 public void testSelectNew() throws Exception { 713 assertTranslation( "select new Animal(an.description, an.bodyWeight) from Animal an" ); 714 assertTranslation( "select new org.hibernate.test.hql.Animal(an.description, an.bodyWeight) from Animal an" ); 715 } 716 717 public void testSimpleCorrelatedSubselect() throws Exception { 718 assertTranslation( "from Animal a where a.bodyWeight = (select o.bodyWeight from a.offspring o)" ); 719 assertTranslation( "from Animal a where a = (from a.offspring o)" ); 720 } 721 722 public void testSimpleUncorrelatedSubselect() throws Exception { 723 assertTranslation( "from Animal a where a.bodyWeight = (select an.bodyWeight from Animal an)" ); 724 assertTranslation( "from Animal a where a = (from Animal an)" ); 725 } 726 727 public void testSimpleCorrelatedSubselect2() throws Exception { 728 assertTranslation( "from Animal a where a = (select o from a.offspring o)" ); 729 assertTranslation( "from Animal a where a in (select o from a.offspring o)" ); 730 } 731 732 public void testSimpleUncorrelatedSubselect2() throws Exception { 733 assertTranslation( "from Animal a where a = (select an from Animal an)" ); 734 assertTranslation( "from Animal a where a in (select an from Animal an)" ); 735 } 736 737 public void testUncorrelatedSubselect2() throws Exception { 738 assertTranslation( "from Animal a where a.bodyWeight = (select max(an.bodyWeight) from Animal an)" ); 739 } 740 741 public void testCorrelatedSubselect2() throws Exception { 742 assertTranslation( "from Animal a where a.bodyWeight > (select max(o.bodyWeight) from a.offspring o)" ); 743 } 744 745 public void testManyToManyJoinInSubselect() throws Exception { 746 assertTranslation( "select foo from Foo foo where foo in (select elt from Baz baz join baz.fooArray elt)" ); 747 } 748 749 public void testImplicitJoinInSubselect() throws Exception { 750 assertTranslation( "from Animal a where a = (select an.mother from Animal an)" ); 751 assertTranslation( "from Animal a where a.id = (select an.mother.id from Animal an)" ); 752 } 753 754 public void testManyToOneSubselect() { 755 assertTranslation( "from Animal a where 'foo' in (select m.description from a.mother m)" ); 757 } 758 759 public void testPositionalParameters() throws Exception { 760 assertTranslation( "from Animal an where an.bodyWeight > ?" ); 761 } 762 763 public void testKeywordPropertyName() throws Exception { 764 assertTranslation( "from Glarch g order by g.order asc" ); 765 assertTranslation( "select g.order from Glarch g where g.order = 3" ); 766 } 767 768 public void testJavaConstant() throws Exception { 769 assertTranslation( "from org.hibernate.test.legacy.Category c where c.name = org.hibernate.test.legacy.Category.ROOT_CATEGORY" ); 770 assertTranslation( "from org.hibernate.test.legacy.Category c where c.id = org.hibernate.test.legacy.Category.ROOT_ID" ); 771 } 775 776 public void testClassName() throws Exception { 777 assertTranslation( "from Zoo zoo where zoo.class = PettingZoo" ); 778 assertTranslation( "from DomesticAnimal an where an.class = Dog" ); 779 assertTranslation( "from Animal an where an.class = Dog" ); 780 } 781 782 public void testSelectDialectFunction() throws Exception { 783 if ( getDialect() instanceof HSQLDialect ) { 785 assertTranslation( "select mod(s.count, 2) from org.hibernate.test.legacy.Simple as s where s.id = 10" ); 786 } 788 assertTranslation( "select upper(human.name.first) from Human human" ); 789 assertTranslation( "from Human human where lower(human.name.first) like 'gav%'" ); 790 assertTranslation( "select upper(a.description) from Animal a" ); 791 assertTranslation( "select max(a.bodyWeight) from Animal a" ); 792 } 793 794 public void testTwoJoins() throws Exception { 795 assertTranslation( "from Human human join human.friends, Human h join h.mother" ); 796 assertTranslation( "from Human human join human.friends f, Animal an join an.mother m where f=m" ); 797 assertTranslation( "from Baz baz left join baz.fooToGlarch, Bar bar join bar.foo" ); 798 } 799 800 public void testToOneToManyManyJoinSequence() throws Exception { 801 assertTranslation( "from Dog d join d.owner h join h.friends f where f.name.first like 'joe%'" ); 802 } 803 804 public void testToOneToManyJoinSequence() throws Exception { 805 assertTranslation( "from Animal a join a.mother m join m.offspring" ); 806 assertTranslation( "from Dog d join d.owner m join m.offspring" ); 807 assertTranslation( "from Animal a join a.mother m join m.offspring o where o.bodyWeight > a.bodyWeight" ); 808 } 809 810 public void testSubclassExplicitJoin() throws Exception { 811 assertTranslation( "from DomesticAnimal da join da.owner o where o.nickName = 'gavin'" ); 812 assertTranslation( "from DomesticAnimal da join da.owner o where o.bodyWeight > 0" ); 813 } 814 815 public void testMultipleExplicitCollectionJoins() throws Exception { 816 assertTranslation( "from Animal an inner join an.offspring os join os.offspring gc" ); 817 assertTranslation( "from Animal an left outer join an.offspring os left outer join os.offspring gc" ); 818 } 819 820 public void testSelectDistinctComposite() throws Exception { 821 assertTranslation( "select distinct p from org.hibernate.test.compositeelement.Parent p join p.children c where c.name like 'Child%'" ); 823 } 824 825 public void testDotComponent() throws Exception { 826 assertTranslation( "select fum.id from org.hibernate.test.legacy.Fum as fum where not fum.fum='FRIEND'" ); 828 } 829 830 public void testOrderByCount() throws Exception { 831 assertTranslation( "from Animal an group by an.zoo.id order by an.zoo.id, count(*)" ); 832 } 833 834 public void testHavingCount() throws Exception { 835 assertTranslation( "from Animal an group by an.zoo.id having count(an.zoo.id) > 1" ); 836 } 837 838 public void selectWhereElements() throws Exception { 839 assertTranslation( "select foo from Foo foo, Baz baz where foo in elements(baz.fooArray)" ); 840 } 841 842 public void testCollectionOfComponents() throws Exception { 843 assertTranslation( "from Baz baz inner join baz.components comp where comp.name='foo'" ); 844 } 845 846 public void testNestedComponentIsNull() { 847 assertTranslation( "from Commento c where c.marelo.commento.mcompr is null" ); 849 } 850 851 public void testOneToOneJoinedFetch() throws Exception { 852 assertTranslation( "from org.hibernate.test.onetoone.joined.Person p join fetch p.address left join fetch p.mailingAddress" ); 854 } 855 856 public void testSubclassImplicitJoin() throws Exception { 857 assertTranslation( "from DomesticAnimal da where da.owner.nickName like 'Gavin%'" ); 858 assertTranslation( "from DomesticAnimal da where da.owner.nickName = 'gavin'" ); 859 assertTranslation( "from DomesticAnimal da where da.owner.bodyWeight > 0" ); 860 } 861 862 public void testComponent2() throws Exception { 863 assertTranslation( "from Dog dog where dog.owner.name.first = 'Gavin'" ); 864 } 865 866 public void testOneToOne() throws Exception { 867 assertTranslation( "from User u where u.human.nickName='Steve'" ); 868 assertTranslation( "from User u where u.human.name.first='Steve'" ); 869 } 870 871 public void testSelectClauseImplicitJoin() throws Exception { 872 assertTranslation( "select d.owner.mother.description from Dog d" ); 874 } 875 876 public void testFromClauseImplicitJoin() throws Exception { 877 assertTranslation( "from DomesticAnimal da join da.owner.mother m where m.bodyWeight > 10" ); 878 } 879 880 public void testJoinedSubclassWithOrCondition() { 881 assertTranslation( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" ); 882 } 883 884 public void testImplicitJoinInFrom() { 885 assertTranslation( "from Human h join h.mother.mother.offspring o" ); 886 } 887 888 public void testDuplicateImplicitJoinInSelect() { 889 assertTranslation( "select an.mother.bodyWeight from Animal an join an.mother m where an.mother.bodyWeight > 10" ); 893 assertTranslation( "select an.mother.bodyWeight from Animal an where an.mother.bodyWeight > 10" ); 894 assertTranslation( "select an.mother.bodyWeight from Animal an order by an.mother.bodyWeight" ); 896 } 897 898 public void testConstructorNode() throws Exception { 899 ConstructorNode n = new ConstructorNode(); 900 assertNull( n.getFromElement() ); 901 assertFalse( n.isReturnableEntity() ); 902 } 903 904 public void testIndexNode() throws Exception { 905 IndexNode n = new IndexNode(); 906 Exception ex = null; 907 try { 908 n.setScalarColumnText( 0 ); 909 } 910 catch ( UnsupportedOperationException e ) { 911 ex = e; 912 } 913 assertNotNull( ex ); 914 } 915 916 public void testExceptions() throws Exception { 917 DetailedSemanticException dse = new DetailedSemanticException( "test" ); 918 dse.printStackTrace(); 919 dse.printStackTrace( new PrintWriter ( new StringWriter () ) ); 920 QuerySyntaxException qse = new QuerySyntaxException( new RecognitionException( "test" ), "from bozo b where b.clown = true" ); 921 assertNotNull( qse.getMessage() ); 922 } 923 924 public static Test suite() { 925 return new TestSuite( HQLTest.class ); 926 } 927 928 929 public void testSelectProperty2() throws Exception { 930 assertTranslation( "select an, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); 931 assertTranslation( "select an, mo, an.bodyWeight, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); 932 } 933 934 public void testSubclassWhere() throws Exception { 935 assertTranslation( "from PettingZoo pz1, PettingZoo pz2 where pz1.id = pz2.id" ); 937 assertTranslation( "from PettingZoo pz1, PettingZoo pz2 where pz1.id = pz2" ); 938 assertTranslation( "from PettingZoo pz where pz.id > 0 " ); 939 } 940 941 public void testNestedImplicitJoinsInSelect() throws Exception { 942 assertTranslation( "select foo.foo.foo.foo.string from org.hibernate.test.legacy.Foo foo" ); 948 } 949 950 public void testNestedComponent() throws Exception { 951 assertTranslation( "from org.hibernate.test.legacy.Foo foo where foo.component.subcomponent.name='bar'" ); 954 } 955 956 public void testNull2() throws Exception { 957 assertTranslation( "from Human h where not( h.nickName is null )" ); 959 assertTranslation( "from Human h where not( h.nickName is not null )" ); 960 } 961 962 public void testUnknownFailureFromMultiTableTest() { 963 assertTranslation( "from Lower s where s.yetanother.name='name'" ); 964 } 965 966 public void testJoinInSubselect() throws Exception { 967 DotNode.useThetaStyleImplicitJoins = true; 969 assertTranslation( "from Animal a where a in (select m from Animal an join an.mother m)" ); 970 assertTranslation( "from Animal a where a in (select o from Animal an join an.offspring o)" ); 971 DotNode.useThetaStyleImplicitJoins = false; 972 } 973 974 public void testJoinedSubclassImplicitJoin() throws Exception { 975 assertTranslation( "from org.hibernate.test.legacy.Lower s where s.yetanother.name='name'" ); 978 } 979 980 public void testProjectProductJoinedSubclass() throws Exception { 981 assertTranslation( "select zoo from Zoo zoo, PettingZoo pz where zoo=pz" ); 983 assertTranslation( "select zoo, pz from Zoo zoo, PettingZoo pz where zoo=pz" ); 984 } 985 986 public void testCorrelatedSubselect1() throws Exception { 987 assertTranslation( "from Animal a where exists (from a.offspring o where o.bodyWeight>10)" ); 990 } 991 992 public void testOuterAliasInSubselect() { 993 assertTranslation( "from Human h where h = (from Animal an where an = h)" ); 994 } 995 996 public void testFetch() throws Exception { 997 assertTranslation( "from Zoo zoo left join zoo.mammals" ); 998 assertTranslation( "from Zoo zoo left join fetch zoo.mammals" ); 999 } 1000 1001 public void testOneToManyElementFunctionInWhere() throws Exception { 1002 assertTranslation( "from Zoo zoo where 'dog' in indices(zoo.mammals)" ); 1003 assertTranslation( "from Zoo zoo, Dog dog where dog in elements(zoo.mammals)" ); 1004 } 1005 1006 1010 1011 public void testManyToManyInJoin() throws Exception { 1012 assertTranslation( "select x.id from Human h1 join h1.family x" ); 1013 } 1015 1016 public void testManyToManyInSubselect() throws Exception { 1017 assertTranslation( "from Human h1, Human h2 where h2 in (select x.id from h1.family x)" ); 1018 assertTranslation( "from Human h1, Human h2 where 'father' in indices(h1.family)" ); 1019 } 1020 1021 public void testOneToManyIndexAccess() throws Exception { 1022 assertTranslation( "from Zoo zoo where zoo.mammals['dog'] is not null" ); 1023 } 1024 1025 public void testImpliedSelect() throws Exception { 1026 assertTranslation( "select zoo from Zoo zoo" ); 1027 assertTranslation( "from Zoo zoo" ); 1028 assertTranslation( "from Zoo zoo join zoo.mammals m" ); 1029 assertTranslation( "from Zoo" ); 1030 assertTranslation( "from Zoo zoo join zoo.mammals" ); 1031 } 1032 1033 public void testVectorSubselect() { 1034 assertTranslation( "from Animal a where ('foo', 'bar') in (select m.description, m.bodyWeight from a.mother m)" ); 1035 } 1036 1037 public void testWierdSubselectImplicitJoinStuff() { 1038 assertTranslation("from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0"); 1040 } 1041 1042 1047 1048 1056 1057 1060 public void testCollectionsInSelect2() throws Exception { 1061 assertTranslation( "select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string" ); 1063 } 1064 1065 1066 1096 1098 1100} 1101 | Popular Tags |