|
Home TOC Index |
|
Search
Feedback |
Example Queries
The following queries are from the
PlayerEJBentity bean of theRosterAppJ2EEapplication, which is documented in the Chapter 6. To see the relationships between the beans of the
RosterApp, see Figure 6-1.Simple Finder Queries
If you are unfamiliar with EJB QL, these simple queries are a good place to start.
Example 1
SELECT OBJECT(p) FROM Player pDescription: The
FROMclause declares an identification variable namedp, omitting the optional keywordAS. If theASkeyword were included, the clause would be written as follows:FROM Player AS pThe
Playerelement is the abstract schema name of thePlayerEJBentity bean. Because the bean defines thefindallmethod in theLocalPlayerHomeinterface, the objects returned by the query have theLocalPlayertype.See also: Identification Variables
Example 2
SELECT DISTINCT OBJECT(p) FROM Player p WHERE p.position = ?1Data 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. TheDISTINCTkeyword eliminates duplicate values.The
WHEREclause restricts the players retrieved by checking theirposition, a persistent field of thePlayerEJBentity bean. The?1element denotes the input parameter of thefindByPositionmethod.See also: Input Parameters, DISTINCT and OBJECT Keywords
Example 3
SELECT DISTINCT OBJECT(p) FROM Player p WHERE p.position = ?1 AND p.name = ?2Data retrieved: The players with the specified position and name.
Finder method:
findByPositionAndName(String position, String name)Description: The
positionandnameelements are persistent fields of thePlayerEJBentity bean. TheWHEREclause compares the values of these fields with the parameters of thefindByPositionAndNamemethod. EJB QL denotes an input parameter with 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 4
SELECT DISTINCT OBJECT(p) FROM Player p, IN (p.teams) AS t WHERE t.city = ?1Data 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 thePlayerEJBentity bean, and thetvariable represents the relatedTeamEJBbeans. The declaration fortreferences the previously declaredpvariable. TheINkeyword signifies thatteamsis a collection of related beans. Thep.teamsexpression navigates from aPlayerEJBbean to its relatedTeamEJBbeans. 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 may not 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 5
SELECT DISTINCT OBJECT(p) FROM Player p, IN (p.teams) AS t WHERE t.league = ?1Data 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 thePlayerEJB-TeamEJBrelationship, and thet.leagueexpression navigates theTeamEJB-LeagueEJBrelationship.In the other examples, the input parameters are
Example 6Stringobjects, but in this example the parameter is an object whose type is aLocalLeagueinterface. This type matches theleaguerelationship field in the comparison expression of theWHEREclause.
SELECT DISTINCT OBJECT(p) FROM Player p, IN (p.teams) AS t WHERE t.league.sport = ?1Data retrieved: The players who participate in the specified sport.
Finder method:
findBySport(String sport)Description: The
sportpersistent field belongs to theLeagueEJBbean. To reach thesportfield, the query must first navigate from thePlayerEJBbean to theTeamEJBbean (p.teams) and then from theTeamEJBbean to theLeagueEJBbean (t.league). Because theleaguerelationship field is not a collection, it may be followed by thesportpersistent field.Finder Queries with Other Conditional Expressions
Every
Example 7WHEREclause 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.
SELECT OBJECT(p) FROM Player p WHERE p.teams IS EMPTYData retrieved: All players who do not belong to a team.
Finder method:
findNotOnTeam()Description: The
teamsrelationship field of thePlayerEJBbean 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 8
SELECT DISTINCT OBJECT(p) FROM Player p WHERE p.salary BETWEEN ?1 AND ?2Data 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:p.salary >= ?1 AND p.salary <= ?2See also: BETWEEN Expressions
Example 9
SELECT DISTINCT OBJECT(p1) FROM Player p1, Player p2 WHERE p1.salary > p2.salary AND p2.name = ?1Data 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 may return persistent fields or other entity beans.
Example 10
SELECT DISTINCT t.league FROM Player p, IN (p.teams) AS t WHERE p = ?1Data 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
LeagueEJBentity 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 11
SELECT DISTINCT t.league.sport FROM Player p, IN (p.teams) AS t WHERE p = ?1Data 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 theLeagueEJBentity bean.
|
Home TOC Index |
|
Search
Feedback |