Download
FAQ History |
API
Search Feedback |
Example Queries
The following queries are from the
PlayerBean
entity bean of theRosterApp
J2EE application, which is documented in Chapter 27. To see the relationships between the beans of theRosterApp
, see Figure 27-3.Simple Finder Queries
If you are unfamiliar with EJB QL, these simple queries are a good place to start.
Example 1Data retrieved: All players.
Finder method:
findall()
Description: The
FROM
clause declares an identification variable namedp
, omitting the optional keywordAS
. If theAS
keyword were included, the clause would be written as follows:The
Player
element is the abstract schema name of thePlayerBean
entity bean. Because the bean defines thefindall
method in theLocalPlayerHome
interface, the objects returned by the query have theLocalPlayer
type.See also: Identification Variables
Example 2Data retrieved: The players with the position specified by the finder method's parameter.
Finder method:
findByPosition(String position)
Description: In a
SELECT
clause, theOBJECT
keyword must precede a stand-alone identification variable such asp
. (A stand-alone identification variable is not part of a path expression.) TheDISTINCT
keyword eliminates duplicate values.The
WHERE
clause restricts the players retrieved by checking theirposition
, a persistent field of thePlayerBean
entity bean. The?1
element denotes the input parameter of thefindByPosition
method.See also: Input Parameters, DISTINCT and OBJECT Keywords
Example 3Data retrieved: The players having the specified positions and names.
Finder method:
findByPositionAndName(String position, String name)
Description: The
position
andname
elements are persistent fields of thePlayerBean
entity bean. TheWHERE
clause compares the values of these fields with the parameters of thefindByPositionAndName
method. EJB QL denotes an input parameter using a question mark followed by an integer. The first input parameter is?1
, the second is?2
, and so forth.Finder Queries That Navigate to Related Beans
In EJB QL, an expression can traverse (or navigate) to related beans. These expressions are the primary difference between EJB QL and SQL. EJB QL navigates to related beans, whereas SQL joins tables.
Example 4Data retrieved: The players whose teams belong to the specified city.
Finder method:
findByCity(String city)
Description: The
FROM
clause declares two identification variables:p
andt
. Thep
variable represents thePlayerBean
entity bean, and thet
variable represents the relatedTeamBean
beans. The declaration fort
references the previously declaredp
variable. TheIN
keyword signifies thatteams
is a collection of related beans. Thep.teams
expression navigates from aPlayerBean
bean to its relatedTeamBean
beans. The period in thep.teams
expression is the navigation operator.In the
WHERE
clause, the period preceding the persistent variablecity
is a delimiter, not a navigation operator. Strictly speaking, expressions can navigate to relationship fields (related beans), but not to persistent fields. To access a persistent field, an expression uses the period as a delimiter.Expressions cannot navigate beyond (or further qualify) relationship fields that are collections. In the syntax of an expression, a collection-valued field is a terminal symbol. Because the
teams
field is a collection, theWHERE
clause cannot specifyp.teams.city
--an illegal expression.See also: Path Expressions
Example 5Data retrieved: The players that belong to the specified league.
Finder method:
findByLeague(LocalLeague league)
Description: The expressions in this query navigate over two relationships. The
p.teams
expression navigates thePlayerBean
-TeamBean
relationship, and thet.league
expression navigates theTeamBean-LeagueBean
relationship.In the other examples, the input parameters are
String
objects, but in this example the parameter is an object whose type is aLocalLeague
interface. This type matches theleague
relationship field in the comparison expression of theWHERE
clause.Example 6Data retrieved: The players who participate in the specified sport.
Finder method:
findBySport(String sport)
Description: The
sport
persistent field belongs to theLeagueBean
bean. To reach thesport
field, the query must first navigate from thePlayerBean
bean to theTeamBean
bean (p.teams
) and then from theTeamBean
bean to theLeagueBean
bean (t.league
). Because theleague
relationship field is not a collection, it can be followed by thesport
persistent field.Finder 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.Example 7Data retrieved: All players who do not belong to a team.
Finder method:
findNotOnTeam()
Description: The
teams
relationship field of thePlayerBean
bean is a collection. If a player does not belong to a team, then theteams
collection is empty and the conditional expression isTRUE
.See also: Empty Collection Comparison Expressions
Example 8Data retrieved: The players whose salaries fall within the range of the specified salaries.
Finder method:
findBySalaryRange(double low, double high)
Description: This
BETWEEN
expression has three arithmetic expressions: a persistent field (p.salary
) and the two input parameters (?1
and?2
). The following expression is equivalent to theBETWEEN
expression:See also: BETWEEN Expressions
Example 9Data retrieved: All players whose salaries are higher than the salary of the player with the specified name.
Finder method:
findByHigherSalary(String name)
Description: The
FROM
clause declares two identification variables (p1
andp2
) of the same type (Player
). Two identification variables are needed because theWHERE
clause compares the salary of one player (p2
) with that of the other players (p1
).See also: Identification Variables
Select Queries
The queries in this section are for select methods. Unlike finder methods, a select method can return persistent fields or other entity beans.
Example 10Data retrieved: The leagues to which the specified player belongs.
Select method:
ejbSelectLeagues(LocalPlayer player)
Description: The return type of this query is the abstract schema type of the
LeagueBean
entity bean. This abstract schema type maps to theLocalLeagueHome
interface. Because the expressiont.league
is not a stand-alone identification variable, theOBJECT
keyword is omitted.See also: SELECT Clause
Example 11Data retrieved: The sports that the specified player participates in.
Select method:
ejbSelectSports(LocalPlayer player)
Description: This query returns a
String
namedsport
, which is a persistent field of theLeagueBean
entity bean.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.