I’m using OpenFeign/QueryDSL 7.1 with Spring Boot 3.4.4, which should include the fix from this pull request: Add support for subquery modifiers (LIMIT/OFFSET) in Hibernate by @DongGunYoon in #1304
However, I’m still facing the same issue — the LIMIT clause in subqueries seems to be ignored. I’d appreciate any help understanding what I might be doing wrong or how to use it correctly. Here’s the setup and example: Dependencies:
<dependency>
<groupId>io.github.openfeign.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>7.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>7.1</version>
</dependency>
APT Plugin:
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
Example Query:
jpaQueryFactory.selectFrom(qVehicleLocation)
.innerJoin(qVehicleLocation.vehicle).fetchJoin()
.where(
qVehicleLocation.id.eq(
JPAExpressions.select(sub.id)
.from(sub)
.where(sub.vehicle.id.eq(qVehicleLocation.vehicle.id))
.orderBy(sub.createdAt.desc(), sub.id.desc())
.limit(1)
)
)
.fetch();
Generated sub_query SQL:
select vl2_0.id
from vehicle_location vl2_0
where vl2_0.vehicle_id = vl1_0.vehicle_id
order by vl2_0.created_at desc, vl2_0.id desc
As you can see, the LIMIT 1 is missing from the generated SQL.
Could you please advise how to correctly apply LIMIT in a subquery with QueryDSL 7.1 and Hibernate?
Thanks in advance for your help!
Have you tried notExists() instead of id.eq(JPAExpressions.select(...).limit(1)) ?
jpaQueryFactory
.selectFrom(qVehicleLocation)
.innerJoin(qVehicleLocation.vehicle).fetchJoin()
.where(
JPAExpressions.selectOne()
.from(subLocation)
.where(
subLocation.vehicle.eq(qVehicleLocation.vehicle),
subLocation.createdAt.gt(qVehicleLocation.createdAt)
.or(
subLocation.createdAt.eq(qVehicleLocation.createdAt)
.and(subLocation.id.gt(qVehicleLocation.id))
)
)
.notExists()
)
.fetch();