基本上:
select dfrom T_DEBIT dwhere exists ( select r from T_REQUEST r where r.debit.id = d.id and r.status = SUCCESSFUL)
检查JPQL中的枚举语法,我通常不对实体使用枚举,在此示例中可能是错误的。
作为一种样式问题,我将使用实体名称==类名称而不是实体名称==表名称。它使得JPQL 不是 SQL更清晰的事实
更新
Spring要求解决类似问题。解决这些问题的方法非常系统化:
a)仅使用基本过滤器和以下表达式来重写您的问题:
- “存在一些……条件成立”
- “对于所有…条件为真”
b)翻译:
- 这种情况成为
exists (select ... where condition)
- 这种情况成为
not exists (select ... where NOT condition)
在Spring的特定问题中,“排除所有成功请求”,目标不是很明确。如果他/她的意思是“在没有成功请求的情况下获得所有借方”,那么您应该这样做:
a)将问题改写为“获取所有借方,以便 对于所有关联的请求,请求状态都不为SUCCESSFUL ”。b)翻译为
select dfrom T_DEBIT dwhere not exists ( select r from T_REQUEST r where -- This is the join condition, so it should not be negated r.debit.id = d.id and -- This is the actual filtering condition, negate as this is a FOR ALL not (r.status != SUCCESSFUL))
然后,您可以简化最后一个条件,得到:
select dfrom T_DEBIT dwhere not exists ( select r from T_REQUEST r where r.debit.id = d.id and r.status = SUCCESSFUL)



