Java – the correct way to calculate associated objects using jpql

What is the correct way to write this JPA query? I'm just guessing because I can't solve it or find it in my JPA book

Query query=em.createQuery("select m from Meeting m where count(m.attendees) = 0");
return query.getResultList();

I am currently trying to use hibernate, and I got a MySQL error!

ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your
sql Syntax; check the manual that corresponds to your MysqL server version
for the right Syntax to use near ')=0' at line 1

Solution

To strictly answer the title of the question, use size:

Query query=em.createQuery("select m from Meeting m where size(m.attendees) = 0");
return query.getResultList();

From JPA specification:

In the specific case of 0, you can also use is empty

I will test both to determine which is the most effective (check the query plan)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>