6.5.9 Literals
An Expression literal instance is obtained by passing a value to the literal method of the CriteriaBuilder
interface. An Expression instance representing a null is created by the
nullLiteral method of the CriteriaBuilder interface.
Example:
{code:java}
CriteriaQuery<String> q = cb.createQuery(String.class);
Root<Employee> emp = q.from(Employee.class);
Join<Employee, FrequentFlierPlan> fp = emp.join(Employee_.frequentFlierPlan);
q.select(cb.<String>selectCase()
.when(cb.gt(fp.get(FrequentFlierPlan_.annualMiles), 50000), cb.literal("Platinum"))
.when(cb.gt(fp.get(FrequentFlierPlan_.annualMiles), 25000), cb.literal("Silver"))
.otherwise(cb.nullLiteral(String.class)));
{code}
The following Java Persistence query language query is equivalent:
{code:sql}
SELECT CASE WHEN fp.annualMiles > 50000 THEN 'Platinum'
WHEN fp.annualMiles > 25000 THEN 'Gold'
ELSE NULL
END
{code}