Java Reference
In-Depth Information
Listing 11.6. Using expressions to retrieve items for a specific date range
In this listing an expression is constructed that you'll use to retrieve Items with a cre-
atedDate between two dates. To do this, you use both the query root and the meta-mod-
el. The Item_.createDate ensures that you're referencing an attribute that exists and
ensures, through the use of generics, that you use the appropriate types when you invoke
the between method. The entire listing results in the following query going to the data-
base:
SELECT ITEM_ID, BID_END_DATE, BID_START_DATE, CREATEDDATE, DESCRIPTION,
INITIAL_PRICE, ITEM_NAME, PICTURE, STARRATING, SELLER_ID FROM ITEMS WHERE
(CREATEDDATE BETWEEN ? AND ?)
This is a trivial example. But it demonstrates how the criteria API can be used to create
type-safe queries programmatically that are checked for correctness by the Java compiler.
Let's examine joins next.
Joins
The criteria API supports joining of related classes using an inner join by default. A
join is performed either on a Root object or a Join object. Both objects possess a join
method that can take either a singular or collection-based attribute from the meta-mod-
el: SingularAttribute , CollectionAttribute , SetAttribute , ListAt-
tribute , or MapAttribute , respectively. The first join has to be performed on a Root
object and subsequent joins can be performed using returned Join objects.
To demonstrate this functionality, let's consider a summarization page within ActionBazaar
that summarizes a winning bid. The information for this summarization is pulled from the
Order , Item , Bid , and BazaarAccount entities. You could retrieve this information
by traversing the object graph from the Order object to retrieve the Bid , Item , and
Search WWH ::




Custom Search