Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is only a partial answer but it worked for me from a JUnit test. I used SQL queries, not JPA. It's worth trying:</p> <pre><code> Connection connection = getConnection(); SQLTemplates dialect = new PostgresTemplates(); // TODO: use appropriate db template QEventData c = new QEventData("c"); QEventData c2 = new QEventData("c2"); // Use this to hold alias for inner select PathBuilder&lt;Object[]&gt; c2alias = new PathBuilder&lt;Object[]&gt;(Object[].class, "c2alias"); SQLQuery query = new SQLQuery(connection , dialect); return query.from(c).innerJoin( new SQLSubQuery().from(c2) .groupBy(c2.deviceid) .list(c2.device, c2.timestamp.max().as("timestamp")), c2alias ).on(c.deviceid.eq(c2alias.get("deviceid")), c.timestamp.eq(c2alias.get("timestamp"))) .where(c.deviceid.in(1, 2)) .list(c.deviceid, c.timestamp); </code></pre> <p>Note 1: In the list() function, you have to explicitly list the fields to select or you get an error I believe that is due to the use of the PathBuilder inner select alias.</p> <p>Note 2: Other posts indicate this may not work with JPA, just SQL: <a href="https://groups.google.com/forum/#!msg/querydsl/WDX6oB7Ivsg/0fzoLJA35lgJ" rel="nofollow">https://groups.google.com/forum/#!msg/querydsl/WDX6oB7Ivsg/0fzoLJA35lgJ</a></p> <p>Note 3: In as much SQL as I have written, I have not seen the USING clause ever used in practice, for those who do not know it: <a href="http://docs.oracle.com/javadb/10.8.1.2/ref/rrefsqljusing.html" rel="nofollow">http://docs.oracle.com/javadb/10.8.1.2/ref/rrefsqljusing.html</a></p> <p>Note 4: If the above does not work with querydsl/JPA, then consider refactoring the query to use an EXISTS clause.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload