Java 类org.hibernate.hql.QueryTranslatorFactory 实例源码

项目:cacheonix-core    文件:QueryTranslatorTestCase.java   
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 );
}
项目:eurocarbdb    文件:HibernateEntityManager.java   
/**
*   Returns an SQL query string from the given HQL query string.
*/
public static final String translateHql2Sql( String hql_query_string )
{
    SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
    QueryTranslatorFactory qtf = sfi.getSettings().getQueryTranslatorFactory();

    QueryTranslator qt = qtf.createQueryTranslator( 
        "translated_hql_query"
        , hql_query_string
        , emptyMap()
        , sfi 
    );

    qt.compile( emptyMap(), false );

    return qt.getSQLString();
}
项目:ctsms    文件:QueryUtil.java   
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;
}
项目:document-management-system    文件:HibernateUtil.java   
/**
 * 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;
}
项目:cacheonix-core    文件:SettingsFactory.java   
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);
    }
}
项目:cacheonix-core    文件:EJBQLTest.java   
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 );
}
项目:cacheonix-core    文件:EJBQLTest.java   
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;
}
项目:cacheonix-core    文件:HQLTest.java   
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 );
}
项目:haloDao-Hibernate3    文件:HibernateUtils.java   
/**
 * hql转sql
 * 
 * @param hql
 * @return sql语句
 */
public static String hqlToSql(String hql,SessionFactory sessionFactory) {
    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
    QueryTranslatorFactory queryTranslatorFactory = sessionFactoryImpl.getSettings().getQueryTranslatorFactory();
    FilterTranslator filterTranslator = queryTranslatorFactory.createFilterTranslator(hql, hql, Collections.EMPTY_MAP, sessionFactoryImpl);
    filterTranslator.compile(Collections.EMPTY_MAP, false);
    return filterTranslator.getSQLString();
}
项目:haloDao-Hibernate3    文件:HibernateUtils.java   
/**
 *  TODO 占位符方式转sql
 * @param hql
 * @param sessionFactory
 * @return
 */
public static String hqlToSqlByPlaceholder(String hql,SessionFactory sessionFactory) {
    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
    QueryTranslatorFactory queryTranslatorFactory = sessionFactoryImpl.getSettings().getQueryTranslatorFactory();
    FilterTranslator filterTranslator = queryTranslatorFactory.createFilterTranslator(hql, hql, Collections.EMPTY_MAP, sessionFactoryImpl);
    filterTranslator.compile(Collections.EMPTY_MAP, false);
    String sql=filterTranslator.getSQLString();
    return getPlaceholderSql(hql, sql);
}
项目:cacheonix-core    文件:Settings.java   
public QueryTranslatorFactory getQueryTranslatorFactory() {
    return queryTranslatorFactory;
}
项目:cacheonix-core    文件:Settings.java   
void setQueryTranslatorFactory(QueryTranslatorFactory queryTranslatorFactory) {
    this.queryTranslatorFactory = queryTranslatorFactory;
}
项目:cacheonix-core    文件:QueryTranslatorTestCase.java   
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;
}
项目:cacheonix-core    文件:EJBQLTest.java   
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();
}
项目:cacheonix-core    文件:CriteriaHQLAlignmentTest.java   
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] );

}
项目:cacheonix-core    文件:CriteriaClassicAggregationReturnTest.java   
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] );

}