将SQL转换为JPQL

时间:2014-06-12 15:00:56

标签: mysql sql jpql openjpa

我很难搞清楚,如何将以下SQL翻译为JPQL

SELECT * FROM consorder
WHERE
    consorder.redistid LIKE '123%'
HAVING
    MAX(consorder.redistid)    

(就像MySQL下的魅力一样)。

@Query("select c from ConsignmentOrder c" +
       " where c.redistributionId like ?1" +
       " having max(c.redistributionId)")

我得到以下异常:

Caused by: <openjpa-2.2.2-r422266:1468616 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Encountered "max ( c . redistributionId     ) <EOF>" at character 79, but expected: ["(", ")", "*", "+", "-", ".", "/", ":", "<", "<=", "<>", "=", ">", ">=", "?", "ABS", "ALL", "AND", "ANY", "AS",     "ASC", "AVG", "BETWEEN", "BOTH", "BY", "CASE", "COALESCE", "CONCAT", "COUNT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DELETE", "DESC",     "DISTINCT", "EMPTY", "ESCAPE", "EXISTS", "FETCH", "FROM", "GROUP", "HAVING", "IN", "INDEX", "INNER", "IS", "JOIN", "KEY", "LEADING", "LEFT", "LENGTH",     "LIKE", "LOCATE", "LOWER", "MAX", "MEMBER", "MIN", "MOD", "NEW", "NOT", "NULL", "NULLIF", "OBJECT", "OF", "OR", "ORDER", "OUTER", "SELECT", "SET",     "SIZE", "SOME", "SQRT", "SUBSTRING", "SUM", "TRAILING", "TRIM", "TYPE", "UPDATE", "UPPER", "VALUE", "WHERE", <BOOLEAN_LITERAL>, <DATE_LITERAL>, <    DECIMAL_LITERAL>, <IDENTIFIER>, <INTEGER_LITERAL>, <STRING_LITERAL2>, <STRING_LITERAL>, <TIMESTAMP_LITERAL>, <TIME_LITERAL>].
        at org.apache.openjpa.kernel.jpql.JPQL.generateParseException(JPQL.java:13162)
        at org.apache.openjpa.kernel.jpql.JPQL.jj_consume_token(JPQL.java:13036)
        at org.apache.openjpa.kernel.jpql.JPQL.conditional_primary(JPQL.java:1980)
        at org.apache.openjpa.kernel.jpql.JPQL.conditional_factor(JPQL.java:1958)
        at org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1807)
        at org.apache.openjpa.kernel.jpql.JPQL.conditional_expression(JPQL.java:1769)
        at org.apache.openjpa.kernel.jpql.JPQL.having_clause(JPQL.java:1701)
        at org.apache.openjpa.kernel.jpql.JPQL.select_statement(JPQL.java:107)
        at org.apache.openjpa.kernel.jpql.JPQL.parseQuery(JPQL.java:63)
        at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.parse(JPQLExpressionBuilder.java:2401)
        at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.<init>(JPQLExpressionBuilder.java:2388)
        at org.apache.openjpa.kernel.jpql.JPQLParser.parse(JPQLParser.java:49)
        ... 96 moreEncountered "max ( c . redistributionId ) <EOF>" at character 79, but expected: ["(", ")", "*",

任何帮助将不胜感激。

编辑: 我稍微重新构建了一下查询:

 @Query("select c from ConsignmentOrder c where c.redistributionId = (select max(co.redistributionId) from (select co ConsignmentOrder co where co.redistributionId like ?1) as tmp )")

现在异常类似:

Caused by: <openjpa-2.2.2-r422266:1468616 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Encountered "c . redistributionId = ( select max ( co . redistributionId ) from (" at character 40, but expected: ["(", ")", "*", "+", ",", "-", ".", "/", ":", "<", "<=", "<>", "=", ">", ">=", "?", "ABS", "ALL", "AND", "ANY", "AS", "ASC", "AVG", "BETWEEN", "BOTH", "BY", "CASE", "CLASS", "COALESCE", "CONCAT", "COUNT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DELETE", "DESC", "DISTINCT", "ELSE", "EMPTY", "END", "ENTRY", "ESCAPE", "EXISTS", "FETCH", "FROM", "GROUP", "HAVING", "IN", "INDEX", "INNER", "IS", "JOIN", "KEY", "LEADING", "LEFT", "LENGTH", "LIKE", "LOCATE", "LOWER", "MAX", "MEMBER", "MIN", "MOD", "NEW", "NOT", "NULL", "NULLIF", "OBJECT", "OF", "OR", "ORDER", "OUTER", "SELECT", "SET", "SIZE", "SOME", "SQRT", "SUBSTRING", "SUM", "THEN", "TRAILING", "TRIM", "TYPE", "UPDATE", "UPPER", "VALUE", "WHEN", "WHERE", <BOOLEAN_LITERAL>, <DATE_LITERAL>, <DECIMAL_LITERAL>, <IDENTIFIER>, <INTEGER_LITERAL>, <STRING_LITERAL2>, <STRING_LITERAL>, <TIMESTAMP_LITERAL>, <TIME_LITERAL>].

没有误导性的EOF

1 个答案:

答案 0 :(得分:1)

将它转换为JPQL很重要吗?您可以在JPA中轻松运行本机SQL

使用实体管理器中存在的createNativeQuery()方法,您可以使用SQL语法创建可执行查询