Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query a column which value is null in JPA?

Tags:

java

jpa

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.

like image 507
user595234 Avatar asked Jun 12 '11 22:06

user595234


People also ask

Does JPA repository return null?

So while the api in JPA doesn't specify whether the implementation has to return an empty List or always return null.

Is empty in JPA query?

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.

Is null in JPQL?

Following example shows how to use IS NULL to find properties values which have not been set.


1 Answers

Just change your query to

 @NamedQuery(name = "Concept.findByRefTableNull", query = "SELECT c FROM Concept c WHERE c.conceptName = :conceptName and c.refTable IS NULL"),
like image 189
adarshr Avatar answered Sep 18 '22 16:09

adarshr