简体   繁体   English

JPA Criteria API:加入另一个查询

[英]JPA Criteria API: join on another query

I am trying to build this query with JPA Criteria API我正在尝试使用 JPA Criteria API 构建此查询

SELECT s FROM snapshot s
INNER JOIN (
    SELECT collector_id, entity_id, MAX(timestamp) AS "timestamp"
    FROM snapshot GROUP BY collector_id, entity_id
) AS j ON s.TIMESTAMP = j.TIMESTAMP AND s.collector_id = j.collector_id AND s.entity_id = j.entity_id;

The inner select should get 3 properties to identify a snapshot, and then the outer select will get all the other properties of a snapshot based on 3 that inner select returned.内部选择应该获得 3 个属性来标识快照,然后外部选择将根据内部选择返回的 3 获得快照的所有其他属性。

I have success with building the inner select, but how to combine the outer select with the inner using a join?我在构建内部选择方面取得了成功,但是如何使用连接将外部选择与内部选择相结合?

Or, maybe, there is a different way to construct the query itself in a way, that doesn't include a sub query...或者,也许有一种不同的方式来构造查询本身,不包括子查询......

EDIT:编辑:

Similar quertion: jpa criteria-api: join with subselect类似问题: jpa criteria-api: join with subselect

JPA does not support sub-selects in the FROM clause. JPA 不支持 FROM 子句中的子选择。 Some JPA providers may support this.一些 JPA 提供者可能支持这一点。

For example EclipseLink does: http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause例如 EclipseLink 确实: http ://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM