⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hqltest.java

📁 hibernate 开源框架的代码 jar包希望大家能喜欢
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
//$Id: HQLTest.java 11374 2007-03-29 19:09:18Z steve.ebersole@jboss.com $package org.hibernate.test.hql;import java.io.PrintWriter;import java.io.StringWriter;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import antlr.RecognitionException;import junit.framework.Test;import org.hibernate.Hibernate;import org.hibernate.QueryException;import org.hibernate.junit.functional.FunctionalTestClassTestSuite;import org.hibernate.dialect.DB2Dialect;import org.hibernate.dialect.HSQLDialect;import org.hibernate.dialect.MySQLDialect;import org.hibernate.dialect.Oracle9Dialect;import org.hibernate.dialect.PostgreSQLDialect;import org.hibernate.dialect.SybaseDialect;import org.hibernate.dialect.Oracle8iDialect;import org.hibernate.dialect.function.SQLFunction;import org.hibernate.engine.SessionFactoryImplementor;import org.hibernate.engine.query.HQLQueryPlan;import org.hibernate.engine.query.ReturnMetadata;import org.hibernate.hql.QueryTranslator;import org.hibernate.hql.QueryTranslatorFactory;import org.hibernate.hql.ast.ASTQueryTranslatorFactory;import org.hibernate.hql.ast.DetailedSemanticException;import org.hibernate.hql.ast.QuerySyntaxException;import org.hibernate.hql.ast.QueryTranslatorImpl;import org.hibernate.hql.ast.tree.ConstructorNode;import org.hibernate.hql.ast.tree.DotNode;import org.hibernate.hql.ast.tree.IndexNode;import org.hibernate.hql.ast.tree.SelectClause;import org.hibernate.hql.ast.tree.FromReferenceNode;/** * Tests cases where the AST based query translator and the 'classic' query translator generate identical SQL. * * @author Gavin King */public class HQLTest extends QueryTranslatorTestCase {	public HQLTest(String x) {		super( x );	}	public static Test suite() {		return new FunctionalTestClassTestSuite( HQLTest.class );	}	public boolean createSchema() {		return false;	}	public boolean recreateSchemaAfterFailure() {		return false;	}	protected void prepareTest() throws Exception {		super.prepareTest();		SelectClause.VERSION2_SQL = true;		DotNode.REGRESSION_STYLE_JOIN_SUPPRESSION = true;		DotNode.ILLEGAL_COLL_DEREF_EXCP_BUILDER = new DotNode.IllegalCollectionDereferenceExceptionBuilder() {			public QueryException buildIllegalCollectionDereferenceException(String propertyName, FromReferenceNode lhs) {				throw new QueryException( "illegal syntax near collection: " + propertyName );			}		};	}	protected void cleanupTest() throws Exception {		SelectClause.VERSION2_SQL = false;		DotNode.REGRESSION_STYLE_JOIN_SUPPRESSION = false;		DotNode.ILLEGAL_COLL_DEREF_EXCP_BUILDER = DotNode.DEF_ILLEGAL_COLL_DEREF_EXCP_BUILDER;		super.cleanupTest();	}	public void testInvalidCollectionDereferencesFail() {		// should fail with the same exceptions (because of the DotNode.ILLEGAL_COLL_DEREF_EXCP_BUILDER injection)		assertTranslation( "from Animal a where a.offspring.description = 'xyz'" );		assertTranslation( "from Animal a where a.offspring.father.description = 'xyz'" );	}	public void testSubComponentReferences() {		assertTranslation( "select c.address.zip.code from ComponentContainer c" );		assertTranslation( "select c.address.zip from ComponentContainer c" );		assertTranslation( "select c.address from ComponentContainer c" );	}	public void testManyToAnyReferences() {		assertTranslation( "from PropertySet p where p.someSpecificProperty.id is not null" );		assertTranslation( "from PropertySet p join p.generalProperties gp where gp.id is not null" );	}	public void testJoinFetchCollectionOfValues() {		assertTranslation( "select h from Human as h join fetch h.nickNames" );	}	public void testCollectionJoinsInSubselect() {		// caused by some goofiness in FromElementFactory that tries to		// handle correlated subqueries (but fails miserably) even though this		// is not a correlated subquery.  HHH-1248		assertTranslation(				"select a.id, a.description" +				" from Animal a" +				"       left join a.offspring" +				" where a in (" +				"       select a1 from Animal a1" +				"           left join a1.offspring o" +				"       where a1.id=1" +		        ")"		);		assertTranslation(				"select h.id, h.description" +		        " from Human h" +				"      left join h.friends" +				" where h in (" +				"      select h1" +				"      from Human h1" +				"          left join h1.friends f" +				"      where h1.id=1" +				")"		);	}	public void testEmptyInListFailureExpected() {		assertTranslation( "select a from Animal a where a.description in ()" );	}	public void testDateTimeArithmeticReturnTypesAndParameterGuessing() {		QueryTranslatorImpl translator = createNewQueryTranslator( "select o.orderDate - o.orderDate from Order o" );		assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );		assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );		translator = createNewQueryTranslator( "select o.orderDate + 2 from Order o" );		assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );		assertEquals( "incorrect return type", Hibernate.CALENDAR_DATE, translator.getReturnTypes()[0] );		translator = createNewQueryTranslator( "select o.orderDate -2 from Order o" );		assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );		assertEquals( "incorrect return type", Hibernate.CALENDAR_DATE, translator.getReturnTypes()[0] );		translator = createNewQueryTranslator( "from Order o where o.orderDate > ?" );		assertEquals( "incorrect expected param type", Hibernate.CALENDAR_DATE, translator.getParameterTranslations().getOrdinalParameterExpectedType( 1 ) );		translator = createNewQueryTranslator( "select o.orderDate + ? from Order o" );		assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );		assertEquals( "incorrect return type", Hibernate.CALENDAR_DATE, translator.getReturnTypes()[0] );		assertEquals( "incorrect expected param type", Hibernate.DOUBLE, translator.getParameterTranslations().getOrdinalParameterExpectedType( 1 ) );	}	public void testReturnMetadata() {		HQLQueryPlan plan = createQueryPlan( "from Animal a" );		check( plan.getReturnMetadata(), false, true );		plan = createQueryPlan( "select a as animal from Animal a" );		check( plan.getReturnMetadata(), false, false );		plan = createQueryPlan( "from java.lang.Object" );		check( plan.getReturnMetadata(), true, true );		plan = createQueryPlan( "select o as entity from java.lang.Object o" );		check( plan.getReturnMetadata(), true, false );	}	private void check(			ReturnMetadata returnMetadata,	        boolean expectingEmptyTypes,	        boolean expectingEmptyAliases) {		assertNotNull( "null return metadata", returnMetadata );		assertNotNull( "null return metadata - types", returnMetadata );		assertEquals( "unexpected return size", 1, returnMetadata.getReturnTypes().length );		if ( expectingEmptyTypes ) {			assertNull( "non-empty types", returnMetadata.getReturnTypes()[0] );		}		else {			assertNotNull( "empty types", returnMetadata.getReturnTypes()[0] );		}		if ( expectingEmptyAliases ) {			assertNull( "non-empty aliases", returnMetadata.getReturnAliases() );		}		else {			assertNotNull( "empty aliases", returnMetadata.getReturnAliases() );			assertNotNull( "empty aliases", returnMetadata.getReturnAliases()[0] );		}	}	public void testImplicitJoinsAlongWithCartesianProduct() {		DotNode.useThetaStyleImplicitJoins = true;		assertTranslation( "select foo.foo from Foo foo, Foo foo2" );		assertTranslation( "select foo.foo.foo from Foo foo, Foo foo2" );		DotNode.useThetaStyleImplicitJoins = false;	}	public void testSubselectBetween() {		assertTranslation("from Animal x where (select max(a.bodyWeight) from Animal a) between :min and :max");		assertTranslation("from Animal x where (select max(a.description) from Animal a) like 'big%'");		assertTranslation("from Animal x where (select max(a.bodyWeight) from Animal a) is not null");		assertTranslation("from Animal x where exists (select max(a.bodyWeight) from Animal a)");		assertTranslation("from Animal x where (select max(a.bodyWeight) from Animal a) in (1,2,3)");	}	public void testFetchOrderBy() {		assertTranslation("from Animal a left outer join fetch a.offspring where a.mother.id = :mid order by a.description");	}	public void testCollectionOrderBy() {		assertTranslation("from Animal a join a.offspring o order by a.description");		assertTranslation("from Animal a join fetch a.offspring order by a.description");		assertTranslation("from Animal a join fetch a.offspring o order by o.description");		assertTranslation("from Animal a join a.offspring o order by a.description, o.description");	}	public void testExpressionWithParamInFunction() {		assertTranslation("from Animal a where abs(a.bodyWeight-:param) < 2.0");		assertTranslation("from Animal a where abs(:param - a.bodyWeight) < 2.0");		assertTranslation("from Animal where abs(:x - :y) < 2.0");		assertTranslation("from Animal where lower(upper(:foo)) like 'f%'");		if ( ! ( getDialect() instanceof SybaseDialect ) ) {			// SybaseDialect maps the length function -> len; classic translator does not consider that *when nested*			assertTranslation("from Animal a where abs(abs(a.bodyWeight - 1.0 + :param) * abs(length('ffobar')-3)) = 3.0");		}		if ( !( getDialect() instanceof MySQLDialect || getDialect() instanceof SybaseDialect ) ) {			assertTranslation("from Animal where lower(upper('foo') || upper(:bar)) like 'f%'");		}		if ( getDialect() instanceof PostgreSQLDialect ) {			return;		}		assertTranslation("from Animal where abs(cast(1 as float) - cast(:param as float)) = 1.0");	}	public void testCompositeKeysWithPropertyNamedId() {		assertTranslation( "select e.id.id from EntityWithCrazyCompositeKey e" );		assertTranslation( "select max(e.id.id) from EntityWithCrazyCompositeKey e" );	}	public void testMaxindexHqlFunctionInElementAccessorFailureExpected() {		//TODO: broken SQL		//      steve (2005.10.06) - this is perfect SQL, but fairly different from the old parser		//              tested : HSQLDB (1.8), Oracle8i		assertTranslation( "select c from ContainerX c where c.manyToMany[ maxindex(c.manyToMany) ].count = 2" );		assertTranslation( "select c from Container c where c.manyToMany[ maxIndex(c.manyToMany) ].count = 2" );	}	public void testMultipleElementAccessorOperatorsFailureExpected() throws Exception {		//TODO: broken SQL		//      steve (2005.10.06) - Yes, this is all hosed ;)		assertTranslation( "select c from ContainerX c where c.oneToMany[ c.manyToMany[0].count ].name = 's'" );		assertTranslation( "select c from ContainerX c where c.manyToMany[ c.oneToMany[0].count ].name = 's'" );	}	/*public void testSelectMaxElements() throws Exception {		//TODO: this is almost correct, but missing a select-clause column alias!		assertTranslation("select max( elements(one.manies) ) from org.hibernate.test.legacy.One one");	}*/	public void testKeyManyToOneJoinFailureExpected() {		//TODO: new parser generates unnecessary joins (though the query results are correct)		assertTranslation( "from Order o left join fetch o.lineItems li left join fetch li.product p" );		assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" );		assertTranslation( "from Outer o where o.id.master.id.sup.dudu is not null" );	}	public void testDuplicateExplicitJoinFailureExpected() throws Exception {		//very minor issue with select clause:		assertTranslation( "from Animal a join a.mother m1 join a.mother m2" );		assertTranslation( "from Zoo zoo join zoo.animals an join zoo.mammals m" );		assertTranslation( "from Zoo zoo join zoo.mammals an join zoo.mammals m" );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -