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

Viewed 70934

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.

4 Answers

You can use Criteri Api , like this :

private Specification<Object> nullData(String field) {
    return (root, query, cb) ->
            cb.isNull(cb.lower(root.get(field)));
}
Related