Example Queries
The following queries are from the
PlayerBeanentity bean of theRosterAppJ2EE 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
FROMclause declares an identification variable namedp, omitting the optional keywordAS. If theASkeyword were included, the clause would be written as follows:The
Playerelement is the abstract schema name of thePlayerBeanentity bean. Because the bean defines thefindallmethod in theLocalPlayerHomeinterface, the objects returned by the query have theLocalPlayertype.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
SELECTclause, theOBJECTkeyword must precede a stand-alone identification variable such asp. (A stand-alone identification variable is not part of a path expression.) TheDISTINCTkeyword eliminates duplicate values.The
WHEREclause restricts the players retrieved by checking theirposition, a persistent field of thePlayerBeanentity bean. The?1element denotes the input parameter of thefindByPositionmethod.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
positionandnameelements are persistent fields of thePlayerBeanentity bean. TheWHEREclause compares the values of these fields with the parameters of thefindByPositionAndNamemethod. 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
FROMclause declares two identification variables:pandt. Thepvariable represents thePlayerBeanentity bean, and thetvariable represents the relatedTeamBeanbeans. The declaration fortreferences the previously declaredpvariable. TheINkeyword signifies thatteamsis a collection of related beans. Thep.teamsexpression navigates from aPlayerBeanbean to its relatedTeamBeanbeans. The period in thep.teamsexpression is the navigation operator.In the
WHEREclause, the period preceding the persistent variablecityis 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
teamsfield is a collection, theWHEREclause 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.teamsexpression navigates thePlayerBean-TeamBeanrelationship, and thet.leagueexpression navigates theTeamBean-LeagueBeanrelationship.In the other examples, the input parameters are
Stringobjects, but in this example the parameter is an object whose type is aLocalLeagueinterface. This type matches theleaguerelationship field in the comparison expression of theWHEREclause.Example 6Data retrieved: The players who participate in the specified sport.
Finder method:
findBySport(String sport)Description: The
sportpersistent field belongs to theLeagueBeanbean. To reach thesportfield, the query must first navigate from thePlayerBeanbean to theTeamBeanbean (p.teams) and then from theTeamBeanbean to theLeagueBeanbean (t.league). Because theleaguerelationship field is not a collection, it can be followed by thesportpersistent field.Finder Queries with Other Conditional Expressions
Every
WHEREclause 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
teamsrelationship field of thePlayerBeanbean is a collection. If a player does not belong to a team, then theteamscollection 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
BETWEENexpression has three arithmetic expressions: a persistent field (p.salary) and the two input parameters (?1and?2). The following expression is equivalent to theBETWEENexpression: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
FROMclause declares two identification variables (p1andp2) of the same type (Player). Two identification variables are needed because theWHEREclause 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
LeagueBeanentity bean. This abstract schema type maps to theLocalLeagueHomeinterface. Because the expressiont.leagueis not a stand-alone identification variable, theOBJECTkeyword 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
Stringnamedsport, which is a persistent field of theLeagueBeanentity bean.