Jpa Criteria API note
This is a study note of Jpa criteria api from resources of http://www.ibm.com/developerworks/java/library/j-typesafejpa/ (all diagram are linked from this site) and the book Pro JPA 2 2nd Edition.
1. Basic Concepts
A JPA query is a tree of query-expression nodes that contains SQL query clauses such as
ORDER BY. The following diagram shows its constructs.
All constructs are subclass of an
Expression<T> interface. The following is the query expression hierarchy.
Here are some expression examples:
Root<T>is an expression acting as the SQL
FROM. The query will be evaluated against a JPA entity type. A
Root<T>is a special
Path<T>with no parent.
Predicateis a form of query expression that evaluates to either
false. It is the SQL
- A path expression is the result of navigation from a root expression via one or more persistent attribute(s).
JPA uses metamodel classes to describe persistent entity types. It can be generated at development time. A metamodel class is an easier way of refereing to meta information of a persistent entity. For example,
Person_.age is type safe and simple, bettern than
Person.class.getField("age"). For each of the entity type, JPA generates a static public field that is either a
The top level metamodel structure.
The meetamodeal types hierarchy.
The Metamodel attributes hierarchy.
In JPA, a set of mutually referable classes is within a
persistence unit. Metamodel classes are generated by an annotation processor.
3. Build Queries
JPA provides many constructs to build queries.
- Functional Expressions: apply a function to expressions or literal values to create a new expression. For example,
- Complex predicates: such as
in()applies to a variable number of expressions.
- Join expressions: a join expression allows query multiple entities.
- Parameter expresssion: a paramter expression is created with explicit type information.
- Select expression: the
multiselect()expression using projection terms to specify result. There are methods such as
tuple()to shape the result.
4. Build Dynamic Queries
JPA support dynamic query by allowing persistent types and attributes to be referred by names. For example:
Class<Account> cls =Class.forName("domain.Account"); Metamodel model = em.getMetamodel(); EntityType<Account> entity = model.entity(cls); CriteriaQuery<Account> c = cb.createQuery(cls); Root<Account> account = c.from(entity); Path<Integer> balance = account.<Integer>get("balance"); c.where(cb.and (cb.greaterThan(balance, 100), cb.lessThan(balance), 200)));
The APIs are weakly typed. There will be compiler wanrning for unchecked casts.