📄 hqltest.java
字号:
public void testOneToOneJoinedFetch() throws Exception { // From OneToOneTest.testOneToOneOnSubclass assertTranslation( "from org.hibernate.test.onetoone.joined.Person p join fetch p.address left join fetch p.mailingAddress" ); } public void testSubclassImplicitJoin() throws Exception { assertTranslation( "from DomesticAnimal da where da.owner.nickName like 'Gavin%'" ); assertTranslation( "from DomesticAnimal da where da.owner.nickName = 'gavin'" ); assertTranslation( "from DomesticAnimal da where da.owner.bodyWeight > 0" ); } public void testComponent2() throws Exception { assertTranslation( "from Dog dog where dog.owner.name.first = 'Gavin'" ); } public void testOneToOne() throws Exception { assertTranslation( "from User u where u.human.nickName='Steve'" ); assertTranslation( "from User u where u.human.name.first='Steve'" ); } public void testSelectClauseImplicitJoin() throws Exception { //assertTranslation( "select d.owner.mother from Dog d" ); //bug in old qt assertTranslation( "select d.owner.mother.description from Dog d" ); //assertTranslation( "select d.owner.mother from Dog d, Dog h" ); } public void testFromClauseImplicitJoin() throws Exception { assertTranslation( "from DomesticAnimal da join da.owner.mother m where m.bodyWeight > 10" ); } public void testJoinedSubclassWithOrCondition() { assertTranslation( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" ); } public void testImplicitJoinInFrom() { assertTranslation( "from Human h join h.mother.mother.offspring o" ); } public void testDuplicateImplicitJoinInSelect() { // This test causes failures on theta-join dialects because the SQL is different. The old parser // duplicates the condition, whereas the new parser does not. The queries are semantically the // same however. if ( getDialect() instanceof Oracle9Dialect ) return; if ( getDialect() instanceof Oracle8iDialect ) return; assertTranslation( "select an.mother.bodyWeight from Animal an join an.mother m where an.mother.bodyWeight > 10" ); assertTranslation( "select an.mother.bodyWeight from Animal an where an.mother.bodyWeight > 10" ); //assertTranslation("select an.mother from Animal an where an.mother.bodyWeight is not null"); assertTranslation( "select an.mother.bodyWeight from Animal an order by an.mother.bodyWeight" ); } public void testConstructorNode() throws Exception { ConstructorNode n = new ConstructorNode(); assertNull( n.getFromElement() ); assertFalse( n.isReturnableEntity() ); } public void testIndexNode() throws Exception { IndexNode n = new IndexNode(); Exception ex = null; try { n.setScalarColumnText( 0 ); } catch ( UnsupportedOperationException e ) { ex = e; } assertNotNull( ex ); } public void testExceptions() throws Exception { DetailedSemanticException dse = new DetailedSemanticException( "test" ); dse.printStackTrace(); dse.printStackTrace( new PrintWriter( new StringWriter() ) ); QuerySyntaxException qse = QuerySyntaxException.convert( new RecognitionException( "test" ), "from bozo b where b.clown = true" ); assertNotNull( qse.getMessage() ); } public void testSelectProperty2() throws Exception { assertTranslation( "select an, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); assertTranslation( "select an, mo, an.bodyWeight, mo.bodyWeight from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); } public void testSubclassWhere() throws Exception { // TODO: The classic QT generates lots of extra parens, etc. assertTranslation( "from PettingZoo pz1, PettingZoo pz2 where pz1.id = pz2.id" ); assertTranslation( "from PettingZoo pz1, PettingZoo pz2 where pz1.id = pz2" ); assertTranslation( "from PettingZoo pz where pz.id > 0 " ); } public void testNestedImplicitJoinsInSelect() throws Exception { // NOTE: This test is not likely to generate the exact SQL because of the where clause. The synthetic // theta style joins come out differently in the new QT. // From FooBarTest.testQuery() // Missing the foo2_ join, and foo3_ should include subclasses, but it doesn't.// assertTranslation("select foo.foo.foo.foo.string from org.hibernate.test.legacy.Foo foo where foo.foo.foo = 'bar'"); assertTranslation( "select foo.foo.foo.foo.string from org.hibernate.test.legacy.Foo foo" ); } public void testNestedComponent() throws Exception { // From FooBarTest.testQuery() //an extra set of parens in new SQL assertTranslation( "from org.hibernate.test.legacy.Foo foo where foo.component.subcomponent.name='bar'" ); } public void testNull2() throws Exception { //old parser generates useless extra parens assertTranslation( "from Human h where not( h.nickName is null )" ); assertTranslation( "from Human h where not( h.nickName is not null )" ); } public void testUnknownFailureFromMultiTableTest() { assertTranslation( "from Lower s where s.yetanother.name='name'" ); } public void testJoinInSubselect() throws Exception { //new parser uses ANSI-style inner join syntax DotNode.useThetaStyleImplicitJoins = true; assertTranslation( "from Animal a where a in (select m from Animal an join an.mother m)" ); assertTranslation( "from Animal a where a in (select o from Animal an join an.offspring o)" ); DotNode.useThetaStyleImplicitJoins = false; } public void testJoinedSubclassImplicitJoin() throws Exception { // From MultiTableTest.testQueries() // TODO: This produces the proper from clause now, but the parens in the where clause are different. assertTranslation( "from org.hibernate.test.legacy.Lower s where s.yetanother.name='name'" ); } public void testProjectProductJoinedSubclass() throws Exception { // TODO: The old QT generates the discriminator and the theta join in a strange order, and with two extra sets of parens, this is okay, right? assertTranslation( "select zoo from Zoo zoo, PettingZoo pz where zoo=pz" ); assertTranslation( "select zoo, pz from Zoo zoo, PettingZoo pz where zoo=pz" ); } public void testCorrelatedSubselect1() throws Exception { // The old translator generates the theta join before the condition in the sub query. // TODO: Decide if we want to bother generating the theta join in the same order (non simple). assertTranslation( "from Animal a where exists (from a.offspring o where o.bodyWeight>10)" ); } public void testOuterAliasInSubselect() { assertTranslation( "from Human h where h = (from Animal an where an = h)" ); } public void testFetch() throws Exception { assertTranslation( "from Zoo zoo left join zoo.mammals" ); assertTranslation( "from Zoo zoo left join fetch zoo.mammals" ); } public void testOneToManyElementFunctionInWhere() throws Exception { assertTranslation( "from Zoo zoo where 'dog' in indices(zoo.mammals)" ); assertTranslation( "from Zoo zoo, Dog dog where dog in elements(zoo.mammals)" ); } /*public void testManyToManyElementFunctionInSelect() throws Exception { assertTranslation("select elements(zoo.mammals) from Zoo zoo"); assertTranslation("select indices(zoo.mammals) from Zoo zoo"); }*/ public void testManyToManyInJoin() throws Exception { assertTranslation( "select x.id from Human h1 join h1.family x" ); //assertTranslation("select index(h2) from Human h1 join h1.family h2"); } public void testManyToManyInSubselect() throws Exception { assertTranslation( "from Human h1, Human h2 where h2 in (select x.id from h1.family x)" ); assertTranslation( "from Human h1, Human h2 where 'father' in indices(h1.family)" ); } public void testOneToManyIndexAccess() throws Exception { assertTranslation( "from Zoo zoo where zoo.mammals['dog'] is not null" ); } public void testImpliedSelect() throws Exception { assertTranslation( "select zoo from Zoo zoo" ); assertTranslation( "from Zoo zoo" ); assertTranslation( "from Zoo zoo join zoo.mammals m" ); assertTranslation( "from Zoo" ); assertTranslation( "from Zoo zoo join zoo.mammals" ); } public void testVectorSubselect() { assertTranslation( "from Animal a where ('foo', 'bar') in (select m.description, m.bodyWeight from a.mother m)" ); } public void testWierdSubselectImplicitJoinStuff() { //note that the new qt used to eliminate unnecessary join, but no more assertTranslation("from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0"); } /*public void testSelectElementsOfCollectionOfValues() throws Exception { // From FooBarTest.testQuery() // TODO: This produces the where clause in a different order, but it seems okay. assertTranslation("select foo.component.name, elements(foo.component.importantDates) from org.hibernate.test.legacy.Foo foo where foo.foo.id=?"); }*/ //public void testMultiTableElements() throws Exception { /* HQL : select elements(ls.bag), elements(ls.set) from org.hibernate.test.legacy.Lower ls OLD SQL: select top2_.id1_ as col_0_0_, top4_.id1_ as col_1_0_ from leafsubclass lower0_ inner join rootclass lower0_1_ on lower0_.id__=lower0_1_.id1_, simple_simple bag1_, rootclass top2_, rootclass set3_, rootclass top4_ where lower0_1_.id1_ is not null and lower0_.id__=bag1_.simple1 and bag1_.simple2=top2_.id1_ and lower0_.id__=set3_.parent and set3_.id1_=top4_.id1_ */ //assertTranslation("select elements(ls.bag), elements(ls.set) from org.hibernate.test.legacy.Lower ls"); //} public void testCollectionsInSelect2() throws Exception { // This one looks okay now, it just generates extra parens in the where clause. assertTranslation( "select foo.string from Bar bar left join bar.baz.fooArray foo where bar.string = foo.string" ); } //public void testCollectionsInSelect() throws Exception { // From FooBarTest.testCollectionsInSelect /* HQL : select baz, baz.stringSet.size, count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from org.hibernate.test.legacy.Baz baz group by baz OLD SQL: select baz0_.baz_id_column_ as baz_id_c1_, baz0_.count_count as count_co2_37_, baz0_.name_b as name_b37_, baz0_.foo as foo37_, baz0_.superBaz as superBaz37_, baz0_.str as str37_, baz0_.baz_id_column_ as col_0_0_, count(*) as col_1_0_, count(distinct stringset2_.element) as col_2_0_, max(stringset3_.element) as col_3_0_ from baz baz0_, stringSet stringset1_, stringSet stringset2_, stringSet stringset3_ where baz0_.baz_id_column_=stringset1_.id_ and baz0_.baz_id_column_=stringset2_.id_ and baz0_.baz_id_column_=stringset3_.id_ group by baz0_.baz_id_column_ NEW SQL: select // TODO: Remove the extra 'id' column select. baz0_.baz_id_column_ as col_0_0_, // TODO: Figure out how the classic translator knows to use count(*) (select count(*) from stringSet stringset1_ where baz0_.baz_id_column_=stringset1_.id_) as col_1_0_, // This is also correct. count(distinct stringset2_.element) as col_2_0_, max(stringset3_.element) as col_3_0_, // The properties of baz are correct, they're just in the wrong place. baz0_.baz_id_column_ as baz_id_c1_, baz0_.count_count as count_co2_37_, baz0_.name_b as name_b37_, baz0_.foo as foo37_, baz0_.superBaz as superBaz37_, baz0_.str as str37_// FROM is okay. from baz baz0_ stringSet stringset1_, stringSet stringset3_, stringSet stringset2_// WHERE is okay. where (baz0_.baz_id_column_=stringset1_.id_ and baz0_.baz_id_column_=stringset2_.id_ baz0_.baz_id_column_=stringset3_.id_)// GROUP BY is okay. group by baz0_.baz_id_column_ */ //assertTranslation( "select baz, size(baz.stringSet), count( distinct elements(baz.stringSet) ), max( elements(baz.stringSet) ) from org.hibernate.test.legacy.Baz baz group by baz"); //} public void testAssociationPropertyWithoutAlias() throws Exception { // The classic translator doesn't do this right, so don't bother asserting. compileWithAstQueryTranslator("from Animal where zoo is null", false); } private void compileWithAstQueryTranslator(String hql, boolean scalar) { Map replacements = new HashMap(); QueryTranslatorFactory ast = new ASTQueryTranslatorFactory(); SessionFactoryImplementor factory = getSessionFactoryImplementor(); QueryTranslator newQueryTranslator = ast.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, factory ); newQueryTranslator.compile( replacements, scalar ); } public void testComponentNoAlias() throws Exception { // The classic translator doesn't do this right, so don't bother asserting. compileWithAstQueryTranslator( "from Human where name.first = 'Gavin'", false); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -