I am using JPA namedQuery to select data from DB.
@NamedQuery(name = "Concept.findByRefTableNull", query = "SELECT c FROM Concept c WHERE c.conceptName = :conceptName and c.refTable = :refTable"),
///
List<Concept> attributeList
= em.createNamedQuery("Concept.findByRefTableNull")
.setParameter("conceptName", "student").
setParameter("refTable", null).
getResultList();
System.out.println(attributeList.size()); //return 0
The List size is 0, but I am sure it should have records. The reason is the refTable. How to query a column which value is null in JPA ?
Thanks.
So while the api in JPA doesn't specify whether the implementation has to return an empty List or always return null.
The IS EMPTY operator is the logical equivalent of IS NULL, but for collections. Queries can use IS EMPTY operator or IS NOT EMPTY to check whether a collection association path resolves to an empty collection or has at least one value.
Following example shows how to use IS NULL to find properties values which have not been set.
Just change your query to
@NamedQuery(name = "Concept.findByRefTableNull", query = "SELECT c FROM Concept c WHERE c.conceptName = :conceptName and c.refTable IS NULL"),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With