Spring Data JPA and Exists Query

Is Select EXISTS() possible in JPQL?

This answer is obsolete. Please refer to correct answer from Rene Link


No, it is not possible.

Refer to the JPQL BNF documentation from oracle.

simple_cond_expression ::= comparison_expression | between_expression | like_expression | in_expression | null_comparison_expression | empty_collection_comparison_expression | collection_member_expression | exists_expression

exists_expression ::= [NOT] EXISTS(subquery)

Spring Data JPA: Check Exists Entity ID with @Query Annotation

Assuming your Territory entity has a field called "id".

Change your query to:

@Query("SELECT COUNT(e)>0 FROM PlanDetail e WHERE e.territory.id=?1")

JPQL subquery with exists

You need to use r2 instead of r outside the subquery

@Query("SELECT r2 FROM Restaurant r2 
WHERE EXISTS
(SELECT r, COUNT(r) FROM
Restaurant r INNER JOIN r.foods rf WHERE rf IN :foods AND r2=r GROUP BY r
HAVING COUNT(r)=:size)
AND r2.name LIKE '%:name%' AND r2.neighbourhood IN
:neighbourhoods AND r.maxCapacity >= :maxCapacity")


Related Topics



Leave a reply



Submit