与IN运算符一起使用时的JPA CONCATE抛出异常

时间:2015-09-21 12:37:03

标签: java jpa jpql openjpa

我正在尝试运行一个复杂的JPQL QUERY,它不会抛出异常并且工作很奇怪。

所以这是我的查询

SELECT b.pk.col1, b.pk.col2
  FROM table1 b
 WHERE     b.pk.col2 IN ('someString')
       AND b.pk.col3 IN (0, 1, 2)
       AND b.pk.sdate < :now
       AND b.edate > :now
       AND b.col4 = 1
       AND EXISTS
              (SELECT a.cmid
                 FROM table2 a
                WHERE a.col1 = 5 AND a.col2 = b.pk.col1)
       AND b.pk.col1 NOT IN
              (SELECT a.pk.col1
                 FROM table3 a
                WHERE CONCAT (a.pk.tagid, 'ayush') IN ('1ayush'))

由于某些原因,它会在最后一行引发错误。这是错误

  

异常数据:org.apache.openjpa.persistence.ArgumentException:       遇到了#34; b。 pk。 col1 NOT IN(从表3中选择一个.pk.col1 a CONCAT(a.pk.tagId,\&#39; ayush \&#39;)IN       (&#34;字符249,但预计:[&#34;(&#34;,&#34;)&#34;,&#34; *&#34;,&#34; +&# 34;,&#34;,&#34;,&#34; - &#34;,&#34;。&#34;,&#34; /&#34;,&#34;:&# 34;,&#34;&lt;&#34;,&#34;&lt; =&#34;,&#34;&lt;&gt;&#34;,&#34; =&#34;,& #34;&gt;&#34;,&#34;&gt; =&#34;,&#34;?&#34;,&#34; ABS&#34;,&#34; ALL&#34;, &#34; AND&#34;,&#34; ANY&#34;,&#34; AS&#34;,&#34; ASC&#34;,&#34; AVG&#34;,       &#34; BETWEEN&#34;,&#34; BOTH&#34;,&#34; BY&#34;,&#34; CONCAT&#34;,&#34; COUNT&#34;,&#34; CURRENT_DATE& #34;,&#34; CURRENT_TIME&#34;,&#34; CURRENT_TIMESTAMP&#34;,&#34; DELETE&#34;,&#34; DESC&#34;,&#34; DISTINCT&#34;,& #34; EMPTY&#34;,&#34; ESCAPE&#34;,&#34; EXISTS&#34;,&#34; FETCH&#34;,&#34; FROM&#34;,       &#34; GROUP&#34;,&#34; HAVING&#34;,&#34; IN&#34;,&#34; INNER&#34;,&#34; IS&#34;,&#34;加入& #34;,&#34; LEADING&#34;,&#34; LEFT&#34;,&#34; LENGTH&#34;,&#34; LIKE&#34;,&#34; LOCATE&#34;,& #34; LOWER&#34 ;,       &#34; MAX&#34;,&#34;会员&#34;,&#34; MIN&#34;,&#34; MOD&#34;,&#34; NEW&#34;,&#34; NOT& #34;,&#34; NULL&#34;,&#34; OBJECT&#34;,&#34; OF&#34;,&#34; OR&#34;,&#34; ORDER&#34;,& #34; OUTER&#34;,&#34; SELECT&#34;,&#34; SET&#34;,&#34; SIZE&#34;,&#34; SOME&#34;,&#34; SQRT&# 34;,&#34; SUBSTRING&#34;,&#34; SUM&#34;,&#34; TRAILING&#34;,&#34; TRIM&#34;,&#34; UPDATE&#34;,&# 34; UPPER&#34;,&#34; WHERE&#34; ,,,,,]。

为了清晰起见,我已将查询分成多行:

这是一行中的查询

    SELECT b.pk.col1, b.pk.col2
  FROM table1 b
 WHERE     b.pk.col2 IN ('someString')
       AND b.pk.col3 IN (0, 1, 2)
       AND b.pk.sdate < :now
       AND b.edate > :now
       AND b.col4 = 1
       AND EXISTS
              (SELECT a.cmid
                 FROM table2 a
                WHERE a.col1 = 5 AND a.col2 = b.pk.col1)
       AND b.pk.col1 NOT IN
              (SELECT a.pk.col1
                 FROM table3 a
                WHERE CONCAT (a.pk.tagid, 'ayush') IN ('1ayush'))

我忘了提及如果我尝试使用=而不是IN,则查询有效,但这是一个演示查询,我实际上必须使用List

编辑: 这是我的新JPA QUERY,我给它一个Collection而不是一些硬编码值

SELECT b.pk.col1, b.pk.col2
  FROM table1 b
 WHERE     b.pk.col2 IN ('someString')
       AND b.pk.col3 IN (0, 1, 2)
       AND b.pk.sdate < :now
       AND b.edate > :now
       AND b.col4 = 1
       AND EXISTS
              (SELECT a.cmid
                 FROM table2 a
                WHERE a.col1 = 5 AND a.col2 = b.pk.col1)
       AND (b.pk.col1 NOT IN
               (SELECT a.pk.col1
                  FROM table3 a
                 WHERE CONCAT (a.pk.tagid, 'ayush') IN (:somelist)))

我仍然得到相同的例外

  

异常数据:org.apache.openjpa.persistence.ArgumentException:遇到&#34; b。 pk。 col1 NOT IN(从表3中选择一个.pk.col1 a CONCAT(a.pk.tagId,\&#39; ayush \&#39;)IN&#34;在249字符处,但预期:[&#34; (&#34;,&#34;)&#34;,&#34; *&#34;,&#34; +&#34;,&#34;,&#34;,&#34; - &#34;,&#34;。&#34;,&#34; /&#34;,&#34;:&#34;,&#34;&lt;&#34;,&#34 ;&lt; =&#34;,&#34;&lt;&gt;&#34;,&#34; =&#34;,&#34;&gt;&#34;,&#34;&gt; = &#34;,&#34;?&#34;,&#34; ABS&#34;,&#34; ALL&#34;,&#34; AND&#34;,&#34; ANY&#34; ,&#34; AS&#34;,&#34; ASC&#34;,&#34; AVG&#34;,&#34; BETWEEN&#34;,&#34; BOTH&#34;,&#34; BY&#34;,&#34; CONCAT&#34;,&#34; COUNT&#34;,&#34; CURRENT_DATE&#34;,&#34; CURRENT_TIME&#34;,&#34; CURRENT_TIMESTAMP&#34;, &#34; DELETE&#34;,&#34; DESC&#34;,&#34; DISTINCT&#34;,&#34; EMPTY&#34;,&#34; ESCAPE&#34;,&#34; EXISTS& #34;,&#34; FETCH&#34;,&#34; FROM&#34;,&#34; GROUP&#34;,&#34; HAVING&#34;,&#34; IN&#34;,& #34; INNER&#34;,&#34; IS&#34;,&#34; JOIN&#34;,&#34; LEADING&#34;,&#34; LEFT&#34;,&#34; LENGTH&# 34;,&#34; LIKE&#34;,&#34; LOCATE&#34;,&#34; LOWER&#34;,&#34; MAX&#34;,&#34; MEMB ER&#34;,&#34; MIN&#34;,&#34; MOD&#34;,&#34; NEW&#34;,&#34; NOT&#34;,&#34; NULL&#34;, &#34; OBJECT&#34;,&#34; OF&#34;,&#34; OR&#34;,&#34; ORDER&#34;,&#34; OUTER&#34;,&#34; SELECT& #34;,&#34; SET&#34;,&#34; SIZE&#34;,&#34; SOME&#34;,&#34; SQRT&#34;,&#34; SUBSTRING&#34;,& #34; SUM&#34;,&#34; TRAILING&#34;,&#34; TRIM&#34;,&#34; UPDATE&#34;,&#34; UPPER&#34;,&#34; WHERE&# 34; ,,,,,]。       at org.apache.openjpa.kernel.jpql.JPQL.generateParseException(JPQL.java:9566)       at org.apache.openjpa.kernel.jpql.JPQL.jj_consume_token(JPQL.java:9443)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_primary(JPQL.java:1947)       at org.apache.openjpa.kernel.jpql.JPQL.conditional_factor(JPQL.java:1925)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1791)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_expression(JPQL.java:1753)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_primary(JPQL.java:1942)       at org.apache.openjpa.kernel.jpql.JPQL.conditional_factor(JPQL.java:1925)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1791)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1804)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1804)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1804)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1804)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1804)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_term(JPQL.java:1804)       在org.apache.openjpa.kernel.jpql.JPQL.conditional_expression(JPQL.java:1753)       在org.apache.openjpa.kernel.jpql.JPQL.where_clause(JPQL.java:1556)       在org.apache.openjpa.kernel.jpql.JPQL.select_statement(JPQL.java:91)       在org.apache.openjpa.kernel.jpql.JPQL.parseQuery(JPQL.java:63)       在org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder $ ParsedJPQL.parse(JPQLExpressionBuilder.java:1740)       在org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder $ ParsedJPQL。(JPQLExpressionBuilder.java:1727)       在org.apache.openjpa.kernel.jpql.JPQLParser.parse(JPQLParser.java:48)       在org.apache.openjpa.kernel.ExpressionStoreQuery.newCompilation(ExpressionStoreQuery.java:152)       at org.apache.openjpa.kernel.QueryImpl.newCompilation(QueryImpl.java:657)       在org.apache.openjpa.kernel.QueryImpl.compilationFromCache(QueryImpl.java:639)       在org.apache.openjpa.kernel.QueryImpl.compileForCompilation(QueryImpl.java:605)       在org.apache.openjpa.kernel.QueryImpl.compileForExecutor(QueryImpl.java:667)       在org.apache.openjpa.kernel.QueryImpl.getOperation(QueryImpl.java:1492)       在org.apache.openjpa.kernel.DelegatingQuery.getOperation(DelegatingQuery.java:123)       在org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:243)       在org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:294)

1 个答案:

答案 0 :(得分:0)

"xxx IN yyy"应为“{someValue} IN {someCollection}”。您没有右侧的集合,因此例外。如果您的查询现在确实在右侧有一个集合(它不在您的帖子中)并且您仍然获得异常,则发布实际的异常+堆栈跟踪。如果“xxx”和“yyy”应该相等,那么你使用“=”