1

我的 JPQL 查询是这样的:

SELECT blocked 
  FROM BlockedMessage blocked 
  JOIN blocked.resourceDescriptions resource 
 WHERE resource.description IN :resourceDescriptions 
   AND blocked.blockedSince = (SELECT MIN(inner.blockedSince) //error in this line
                                 FROM BlockedMessage inner 
                                 JOIN inner.resourceDescriptions innerResource 
                                WHERE innerResource = resource)

但是 OpenJPA 2.2.2 给出了错误消息:

<openjpa-2.2.2-r422266:1468616 nonfatal user error>
org.apache.openjpa.persistence.ArgumentException: 
Encountered "blocked . blockedSince = ( SELECT MIN ( inner" 
at character 143, 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>].

我没有设法弄清楚使用JPQL 语言参考有什么问题。

4

2 回答 2

0

该查询的问题在于它inner是一个关键字。但是,OpenJPA 的解析器无法报告该关键字在inner.resourceDescriptions.

正确的查询是:

SELECT blocked 
  FROM BlockedMessage blocked 
  JOIN blocked.resourceDescriptions resource 
 WHERE resource.description IN :resourceDescriptions 
   AND blocked.blockedSince <= ALL(SELECT MIN(innerBlocked.blockedSince) 
                                     FROM BlockedMessage innerBlocked 
                                     JOIN innerBlocked.resourceDescriptions innerResource 
                                    WHERE innerResource.description = resource.description)
于 2013-09-19T07:08:31.437 回答
0

我认为您可以在没有子查询的情况下编写查询:

SELECT blocked, MIN(blockedSince)
  FROM BlockedMessage blocked 
  JOIN blocked.resourceDescriptions resource 
 WHERE resource.description IN :resourceDescriptions 
GROUP BY resource

如果您的查询比提供的查询复杂得多并且您必须使用子查询,您还可以考虑在您的服务/java代码中进行两个不同的查询,使用第二个中的第一个产生的数据,因为这样您的查询是细粒度且可重用的。

于 2013-09-17T15:11:04.357 回答