I'm wondering how to convert this query to JPQL:
SELECT t.id, t.title ,
(select count(l.id) from topic_like l where t.id = l.topic_id) as countt
from topics t;
1 Answer
Subqueries in the select and where clauses will work. You can write the same query as:
select t, (select count(l.id) from TopicLikeJpa l where l.topic.id = t.id)
from TopicJpa t
you can replace select t with select t.id, t.name if you don't want to load the whole entity.
But I think you can rewrite the same query without subqueries as
select t, count(l)
from TopicJpa t
left join t.topicLikeJpa l
group by t.id
or
select t.id, t.title, count(l)
from TopicJpa t
left join t.topicLikeJpa l
group by t.id, t.title