Java Reference
In-Depth Information
j2.on(builder.like(itemRoot.get(Item_.itemName),"boat"));
The criteria API also supports fetch joins, which are used to specify an association or at-
tribute that's to be fetched as a part of the query. For example, an order contains a reference
to a bid; to fetch both at the same time, you'd construct the following query:
orderRoot.fetch(Order_.bid,JoinType.INNER);
Now that you have a handle on the query root, let's take a closer look at the
FROM
clause.
11.2.5. FROM clause
The
FROM
clause is dynamically created by the criteria API taking into account your query
roots and joins. There's no need to explicitly write a
FROM
clause—JPA will do all of the
heavily lifting for you. JPA will look at both the query roots you've requested and the joins
that you've specified and construct the
FROM
clause.
11.2.6. SELECT clause
The
SELECT
clause controls the output of the query. The
SELECT
clause is extremely
flexible, providing several different approaches to retrieving data. It's configured using the
select
method on the
CriteriaQuery
object. The deceptively simple
select
meth-
od takes a
Selection
instance as its sole parameter. As you'll see, there are quite a
few subclasses of
Selection
that you can use to construct complex queries. An entire
chapter could be devoted to covering just the
SELECT
clause in more detail. Consequently,
we'll only skim the surface.
The
select
method enables you to retrieve data in several different representations de-
pending on your needs. You aren't limited to only retrieving JPA entities—far from it, in
fact. You can retrieve entities, values, and multiple values, and as you saw earlier, you can
synthesize new objects from the results of a query. You can also work with tuples, which is
an ordered list of values, if you don't want to go through the trouble of creating a wrapper
entity.