Search code examples
spring-dataquerydsl

QueryDSL - add subquery into FROM statement


I need to implement sql query like:

SELECT * FROM (SELECT a FROM b WHERE a.z = 1) WHERE rownum <=1;

How can I write such statement with QueryDSL (I am not using JPA and JDO - only clean sql)?


Solution

  • Querydsl SQL emulates paging of all the supports databases, so you can write directly

    query.from(a)
        .where(a.z.eq(1))
        .limit(1)
        .list(a);
    

    If you need to write this via a subquery then like this

    query.from(
      new SQLSubQuery().from(a).where(a.z.eq(1)).list(a).as(a))
     .where(rownum.loe(1))
     .list(a);