An Expression object is used to in a query's select, where, or having clause
Table 22–1 Conditional Methods in the Expression Interface
Method |
Description |
---|---|
isNull |
Tests whether an expression is null. |
isNotNull |
Tests whether an expression is not null. |
in |
Tests whether an expression is within a list of values. |
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); Metamodel m = em.getMetamodel(); EntityType<Pet> Pet_ = m.entity(Pet.class); Root<Pet> pet = cq.from(Pet.class); cq.where(pet.get(Pet_.color).isNull());
This query finds all pets where the color attribute is null.
CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); Metamodel m = em.getMetamodel(); EntityType<Pet> Pet_ = m.entity(Pet.class); Root<Pet> pet = cq.from(Pet.class); cq.where(pet.get(Pet_.color).in("brown", "black");
This query finds all brown and black pets.
The in method also can check whether an attribute is a member of a collection.