SpringData:查询批注中是否可以有子查询? [英] SpringData : is it possible to have subqueries in the Query annotation?

查看:54
本文介绍了SpringData:查询批注中是否可以有子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在@Query批注(org.springframework.data.jpa.repository.Query;)中进行子查询

I would like to know if it is possible to have subquery in a @Query annotation (org.springframework.data.jpa.repository.Query;)

我在第一个子查询括号中得到QuerySyntaxException.

I am getting a QuerySyntaxException on the first subquery parentesis.

这是我的查询

@Query(value="select c1 from ComplaintModel c1, "
+ "(select c2.id, min(cb.termDate) minDate from ComplaintModel c2 "
+ "join c2.complaintBullets cb join cb.status s where s.code = ?1 "
+ "group by c2.id) tmp where c1.id = tmp.id order by tmp.minDate")

谢谢!

推荐答案

否,JPQL查询的select子句中不能包含子查询.

No, it is not possible to have subquery in the select clause in JPQL query.

JPQL在WHERE和HAVING子句中支持子查询.它可以(至少)是ANY,SOME,ALL,IN,EXIST表达式的一部分,并且当然可以用于常规条件表达式:

JPQL supports subqueries in WHERE and HAVING clauses. It can be (at least) part of ANY, SOME, ALL, IN, EXIST expressions, and of course it can be used normal conditional expressions:

SELECT a
FROM A a
WHERE a.val = (SELECT b.someval 
               FROM B b 
               WHERE b.someotherval=3)

这篇关于SpringData:查询批注中是否可以有子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆