JPQL query --- how to use 'is null'

I use the following query in JPQL to query people whose address column is empty.

List rl = em.createQuery ("select o from Person as o where o.address IS NULL") .setFirstResult (0) .setMaxResults (50) .getResultList (); ...

this line of code always returns an empty list, it is obvious that the table has records matching the condition.

class Person {Address Address; Line name; ...} class Address {String name; ...}

Does anyone know what happened to this jpql statement? thanks in advance.

+7
source share
1 answer

As already mentioned, the address column is empty, and then try using the IS EMPTY expression instead of IS NULL .

em.createQuery( "SELECT o FROM Person o where (o.address.id IS NULL OR o.address.id = 0").setMaxResults(50).getResultList(); 

Check restriction according to id data type.

Also, there is no need to mention setFirstResult (0) , since it is not going to skip any results without it, by default all relevant results will be obtained.

+14
source

All Articles