An Expression object is used in a query's SELECT, WHERE, or HAVING clause. Table 23–1 shows conditional methods you can use with Expression objects.
Table 23–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 |
The following query uses the Expression.isNull method to find 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).isNull());
The following query uses the Expression.in method to find all brown and black pets:
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");
The in method also can check whether an attribute is a member of a collection.