Suqueries in select clause with JPA

白昼怎懂夜的黑 提交于 2019-12-13 15:22:58

问题


I need to execute a subquery in a select clause with Apache Openjpa 2. Does JPA support subqueries in SELECT clause?

My Query is something like this:

SELECT t.date, t.value, 
  (SELECT COUNT(DISTINCT t2.value2) FROM table t2 WHERE t2.date = t.date)
FROM table t
WHERE ...

When I execute my query, I get a class cast exception:

Exception in thread "main" <openjpa-2.1.1-SNAPSHOT-r422266:1141200 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: 
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:872)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:794)
    at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
    at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:315)
    at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:331)
Caused by: java.lang.ClassCastException: org.apache.openjpa.jdbc.sql.LogicalUnion$UnionSelect incompatible with org.apache.openjpa.jdbc.sql.SelectImpl
    at org.apache.openjpa.jdbc.sql.SelectImpl.setParent(SelectImpl.java:579)
    at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.newSelect(SelectConstructor.java:147)
    at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.evaluate(SelectConstructor.java:87)
    at org.apache.openjpa.jdbc.kernel.exps.SubQ.appendTo(SubQ.java:209)
    at org.apache.openjpa.jdbc.kernel.exps.SubQ.appendTo(SubQ.java:203)
    at org.apache.openjpa.jdbc.kernel.exps.SubQ.newSQLBuffer(SubQ.java:167)
    at org.apache.openjpa.jdbc.kernel.exps.SubQ.selectColumns(SubQ.java:153)
    at org.apache.openjpa.jdbc.kernel.exps.SubQ.select(SubQ.java:148)
    at org.apache.openjpa.jdbc.kernel.exps.SelectConstructor.select(SelectConstructor.java:372)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.populateSelect(JDBCStoreQuery.java:295)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.access$100(JDBCStoreQuery.java:86)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery$1.select(JDBCStoreQuery.java:267)
    at org.apache.openjpa.jdbc.sql.LogicalUnion.select(LogicalUnion.java:297)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.populateUnion(JDBCStoreQuery.java:265)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreQuery.executeQuery(JDBCStoreQuery.java:211)
    at org.apache.openjpa.kernel.ExpressionStoreQuery$DataStoreExecutor.executeQuery(ExpressionStoreQuery.java:782)
    at org.apache.openjpa.datacache.QueryCacheStoreQuery$QueryCacheExecutor.executeQuery(QueryCacheStoreQuery.java:346)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1005)
    at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
    ... 6 more

Is this possible or do I have to use NativeQuery / Single Queries?


回答1:


No, it is not supported to use subqueries in SELECT clause. In JPA 2.0 specification this is told with following words:

Subqueries may be used in the WHERE or HAVING clauses.



来源:https://stackoverflow.com/questions/11292651/suqueries-in-select-clause-with-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!