org.hibernate.hql.QueryTranslatorFactory Java Examples

The following examples show how to use org.hibernate.hql.QueryTranslatorFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryTranslatorTestCase.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void runClassicTranslator(String hql) throws Exception {
	SessionFactoryImplementor factory = getSessionFactoryImplementor();
	Map replacements = new HashMap();
	QueryTranslator oldQueryTranslator = null;
	try {
		QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
		oldQueryTranslator = classic.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, factory );
		oldQueryTranslator.compile( replacements, false );
	}
	catch ( Exception e ) {
		e.printStackTrace();
		throw e;
	}
	String oldsql = oldQueryTranslator.getSQLString();
	System.out.println( "HQL    : " + hql );
	System.out.println( "OLD SQL: " + oldsql );
}
 
Example #2
Source File: QueryUtil.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static String hqlToSql(String hqlQueryText, SessionFactory sessionFactory) {
	if (hqlQueryText != null && hqlQueryText.trim().length() > 0
			&& sessionFactory != null) {
		final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
		final SessionFactoryImplementor factory = (SessionFactoryImplementor) sessionFactory;
		final QueryTranslator translator = translatorFactory
				.createQueryTranslator(hqlQueryText, hqlQueryText,
						Collections.EMPTY_MAP, factory);
		translator.compile(Collections.EMPTY_MAP, false);
		return translator.getSQLString();
	}
	return null;
}
 
Example #3
Source File: HibernateUtil.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * HQL to SQL translator
 */
public static String toSql(String hql) {
	if (hql != null && hql.trim().length() > 0) {
		final QueryTranslatorFactory qtf = new ASTQueryTranslatorFactory();
		final SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
		final QueryTranslator translator = qtf.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, sfi);
		translator.compile(Collections.EMPTY_MAP, false);
		return translator.getSQLString();
	}

	return null;
}
 
Example #4
Source File: SettingsFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected QueryTranslatorFactory createQueryTranslatorFactory(Properties properties) {
	String className = PropertiesHelper.getString(
			Environment.QUERY_TRANSLATOR, properties, "org.hibernate.hql.ast.ASTQueryTranslatorFactory"
	);
	log.info("Query translator: " + className);
	try {
		return (QueryTranslatorFactory) ReflectHelper.classForName(className).newInstance();
	}
	catch (Exception cnfe) {
		throw new HibernateException("could not instantiate QueryTranslatorFactory: " + className, cnfe);
	}
}
 
Example #5
Source File: EJBQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void assertEjbqlEqualsHql(String ejbql, String hql) {
	QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();

	QueryTranslator queryTranslator = ast.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, sfi() );
	queryTranslator.compile( Collections.EMPTY_MAP, true );
	String hqlSql = queryTranslator.getSQLString();

	queryTranslator = ast.createQueryTranslator( ejbql, ejbql, Collections.EMPTY_MAP, sfi() );
	queryTranslator.compile( Collections.EMPTY_MAP, true );
	String ejbqlSql = queryTranslator.getSQLString();

	assertEquals( hqlSql, ejbqlSql );
}
 
Example #6
Source File: EJBQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private QueryTranslatorImpl compile(String input) {
	QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
	QueryTranslator queryTranslator = ast.createQueryTranslator( input, input, Collections.EMPTY_MAP, sfi() );
	queryTranslator.compile( Collections.EMPTY_MAP, true );

	return ( QueryTranslatorImpl ) queryTranslator;
}
 
Example #7
Source File: HQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 );
}
 
Example #8
Source File: Settings.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public QueryTranslatorFactory getQueryTranslatorFactory() {
	return queryTranslatorFactory;
}
 
Example #9
Source File: Settings.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
void setQueryTranslatorFactory(QueryTranslatorFactory queryTranslatorFactory) {
	this.queryTranslatorFactory = queryTranslatorFactory;
}
 
Example #10
Source File: QueryTranslatorTestCase.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private QueryTranslatorImpl createNewQueryTranslator(String hql, Map replacements, boolean scalar, SessionFactoryImplementor factory) {
	QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
	QueryTranslatorImpl newQueryTranslator = ( QueryTranslatorImpl ) ast.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, factory );
	newQueryTranslator.compile( replacements, scalar );
	return newQueryTranslator;
}
 
Example #11
Source File: EJBQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private String toSql(String hql) {
	QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
	QueryTranslator queryTranslator = ast.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, sfi() );
	queryTranslator.compile( Collections.EMPTY_MAP, true );
	return queryTranslator.getSQLString();
}
 
Example #12
Source File: CriteriaHQLAlignmentTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testHQLAggregationReturnType() {
	// EJB3: COUNT returns Long
	QueryTranslatorImpl translator = createNewQueryTranslator( "select count(*) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
	
	translator = createNewQueryTranslator( "select count(h.height) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
			
	// MAX, MIN return the type of the state-field to which they are applied. 
	translator = createNewQueryTranslator( "select max(h.height) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
	
	translator = createNewQueryTranslator( "select max(h.id) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
	
	// AVG returns Double.
	translator = createNewQueryTranslator( "select avg(h.height) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
	
	translator = createNewQueryTranslator( "select avg(h.id) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
	
	translator = createNewQueryTranslator( "select avg(h.bigIntegerValue) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
	
       // SUM returns Long when applied to state-fields of integral types (other than BigInteger);
	    translator = createNewQueryTranslator( "select sum(h.id) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
	
	translator = createNewQueryTranslator( "select sum(h.intValue) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );
	
	// SUM returns Double when applied to state-fields of floating point types; 
	translator = createNewQueryTranslator( "select sum(h.height) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select sum(h.floatValue) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );
	
    // SUM returns BigInteger when applied to state-fields of type BigInteger 
	translator = createNewQueryTranslator( "select sum(h.bigIntegerValue) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.BIG_INTEGER, translator.getReturnTypes()[0] );
	
	// SUM and BigDecimal when applied to state-fields of type BigDecimal.
	translator = createNewQueryTranslator( "select sum(h.bigDecimalValue) from Human h" );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.BIG_DECIMAL, translator.getReturnTypes()[0] );

	// special case to test classicquery special case handling of count(*)
	QueryTranslator oldQueryTranslator = null;
	String hql = "select count(*) from Human h";
	QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
	oldQueryTranslator = classic.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, getSessionFactoryImplementor() );
	oldQueryTranslator.compile( Collections.EMPTY_MAP, true);
	assertEquals( "incorrect return type count", 1, oldQueryTranslator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, oldQueryTranslator.getReturnTypes()[0] );

}
 
Example #13
Source File: CriteriaClassicAggregationReturnTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testClassicHQLAggregationReturnTypes() {
	// EJB3: COUNT returns Long
	QueryTranslatorImpl translator = createNewQueryTranslator( "select count(*) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.INTEGER, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select count(h.height) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.INTEGER, translator.getReturnTypes()[0] );

	// MAX, MIN return the type of the state-field to which they are applied.
	translator = createNewQueryTranslator( "select max(h.height) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select max(h.id) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );

	// AVG returns Float integrals, and otherwise the field type.
	translator = createNewQueryTranslator( "select avg(h.height) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select avg(h.id) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.FLOAT, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select avg(h.bigIntegerValue) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.BIG_INTEGER, translator.getReturnTypes()[0] );

       // SUM returns underlying type sum
	    translator = createNewQueryTranslator( "select sum(h.id) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.LONG, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select sum(h.intValue) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.INTEGER, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select sum(h.height) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.DOUBLE, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select sum(h.floatValue) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.FLOAT, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select sum(h.bigIntegerValue) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.BIG_INTEGER, translator.getReturnTypes()[0] );

	translator = createNewQueryTranslator( "select sum(h.bigDecimalValue) from Human h", sfi() );
	assertEquals( "incorrect return type count", 1, translator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.BIG_DECIMAL, translator.getReturnTypes()[0] );

	// special case to test classicquery special case handling of count(*)
	QueryTranslator oldQueryTranslator = null;
	String hql = "select count(*) from Human h";
	QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
	oldQueryTranslator = classic.createQueryTranslator( hql, hql, Collections.EMPTY_MAP, sfi() );
	oldQueryTranslator.compile( Collections.EMPTY_MAP, true);
	assertEquals( "incorrect return type count", 1, oldQueryTranslator.getReturnTypes().length );
	assertEquals( "incorrect return type", Hibernate.INTEGER, oldQueryTranslator.getReturnTypes()[0] );

}