background image

Queries with Other Conditional Expressions

<< Traversing Relationships with an Input | The IS EMPTY Expression >>
<< Traversing Relationships with an Input | The IS EMPTY Expression >>

Queries with Other Conditional Expressions

Navigating According to Related Fields
SELECT DISTINCT p
FROM Player p, IN (p.teams) t
WHERE t.league.sport = :sport
Data retrieved
: The players who participate in the specified sport.
Description
: The sport persistent field belongs to the League entity. To reach the sport field,
the query must first navigate from the Player entity to Team (p.teams) and then from Team to
the League entity (t.league). Because the league relationship field is not a collection, it can be
followed by the sport persistent field.
Queries with Other Conditional Expressions
Every WHERE clause must specify a conditional expression, of which there are several kinds. In
the previous examples, the conditional expressions are comparison expressions that test for
equality. The following examples demonstrate some of the other kinds of conditional
expressions. For descriptions of all conditional expressions, see the section
"WHERE Clause" on
page 751
.
The LIKE Expression
SELECT p
FROM Player p
WHERE p.name LIKE
'Mich%'
Data retrieved
: All players whose names begin with "Mich."
Description
: The LIKE expression uses wildcard characters to search for strings that match the
wildcard pattern. In this case, the query uses the LIKE expression and the % wildcard to find all
players whose names begin with the string "Mich." For example, "Michael" and "Michelle" both
match the wildcard pattern.
See also
:
"LIKE Expressions" on page 754
The IS NULL Expression
SELECT t
FROM Team t
WHERE t.league IS NULL
Data retrieved
: All teams not associated with a league.
Description
: The IS NULL expression can be used to check if a relationship has been set
between two entities. In this case, the query checks to see if the teams are associated with any
leagues, and returns the teams that do not have a league.
Example Queries
The Java EE 5 Tutorial · September 2007
738