⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ejbql5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 5 页
字号:
functions_returning_numerics ::=    <code class="cCodeBold">LENGTH</code>( string_expression) |    <code class="cCodeBold">LOCATE</code>( string_expression, string_expression    [, arithmetic_expression]) |    <code class="cCodeBold">ABS</code>( arithmetic_expression) |    <code class="cCodeBold">SQRT</code>( arithmetic_expression) |    <code class="cCodeBold">MOD</code>( arithmetic_expression, arithmetic_expression)orderby_clause ::= <code class="cCodeBold">ORDER</code> <code class="cCodeBold">BY</code> orderby_item [, orderby_item]*orderby_item ::= cmp_path_expression [<code class="cCodeBold">ASC</code> |<code class="cCodeBold">DESC</code> ]<a name="wp80940"> </a></pre></div><a name="wp80735"> </a><h3 class="pHeading2">FROM Clause</h3><a name="wp79938"> </a><p class="pBody">The <code class="cCode">FROM</code> clause defines the domain of the query by declaring identification variables. Here is the syntax of the <code class="cCode">FROM</code> clause:</p><a name="wp79939"> </a><p class="pBody">from_clause ::= <span style="font-weight: bold">FROM</span> identification_variable_declaration <br />    [, identification_variable_declaration]*<br /><br /><br />identification_variable_declaration ::=<br />    collection_member_declaration |<br />    range_variable_declaration<br /><br />collection_member_declaration ::= <br /><span style="font-weight: bold">    IN</span> (collection_valued_path_expression) [<span style="font-weight: bold">AS</span>] identifier<br /> <br />range_variable_declaration ::= <br />    abstract_schema_name [<span style="font-weight: bold">AS</span>] identifier</p><a name="wp79940"> </a><h4 class="pHeading3">Identifiers</h4><a name="wp79941"> </a><p class="pBody">An identifier is a sequence of one or more characters. The first character must be a valid first character (letter, <code class="cCode">$</code>, <code class="cCode">_</code>) in an identifier of the Java programming language (hereafter in this chapter called simply &quot;Java&quot;). Each subsequent character in the sequence must be a valid non-first character (letter, digit, <code class="cCode">$</code>, <code class="cCode">_</code>) in a Java identifier. (For details, see the J2SE API documentation of the <code class="cCode">isJavaIdentifierStart</code> and <code class="cCode">isJavaIdentifierPart</code> methods of the <code class="cCode">Character</code> class.) The question mark (<code class="cCode">?</code>) is a reserved character in EJB QL and cannot be used in an identifier. Unlike a Java variable, an EJB QL identifier is not case sensitive.</p><a name="wp79966"> </a><p class="pBody">An identifier cannot be the same as an EJB QL keyword:</p><div align="left"><table border="1" id="wp81538">  <caption></caption>  <tr align="left">    <td><a name="wp81538"> </a><div class="pCellBody"><code class="cCode">AND</code></div><a name="wp81539"> </a><div class="pCellBody"><code class="cCode">AS</code></div><a name="wp81540"> </a><div class="pCellBody"><code class="cCode">ASC</code></div><a name="wp81541"> </a><div class="pCellBody"><code class="cCode">AVG</code></div><a name="wp81542"> </a><div class="pCellBody"><code class="cCode">BETWEEN</code></div><a name="wp81543"> </a><div class="pCellBody"><code class="cCode">BY</code></div><a name="wp81544"> </a><div class="pCellBody"><code class="cCode">COUNT</code></div><a name="wp81545"> </a><div class="pCellBody"><code class="cCode">DESC</code></div><a name="wp81546"> </a><div class="pCellBody"><code class="cCode">DISTINCT</code></div><a name="wp79954"> </a><div class="pCellBody"><code class="cCode">EMPTY</code></div></td>    <td><a name="wp81555"> </a><div class="pCellBody"><code class="cCode">FALSE</code></div><a name="wp81556"> </a><div class="pCellBody"><code class="cCode">FROM</code></div><a name="wp81557"> </a><div class="pCellBody"><code class="cCode">IN</code></div><a name="wp81558"> </a><div class="pCellBody"><code class="cCode">IS</code></div><a name="wp81559"> </a><div class="pCellBody"><code class="cCode">LIKE</code></div><a name="wp81560"> </a><div class="pCellBody"><code class="cCode">MAX</code></div><a name="wp81561"> </a><div class="pCellBody"><code class="cCode">MEMBER</code></div><a name="wp81562"> </a><div class="pCellBody"><code class="cCode">MIN</code></div><a name="wp81563"> </a><div class="pCellBody"><code class="cCode">MOD</code></div><a name="wp79965"> </a><div class="pCellBody"><code class="cCode">NOT</code></div></td>    <td><a name="wp81568"> </a><div class="pCellBody">NULL</div><a name="wp81569"> </a><div class="pCellBody">OBJECT</div><a name="wp81570"> </a><div class="pCellBody">OF</div><a name="wp81571"> </a><div class="pCellBody">OR</div><a name="wp81572"> </a><div class="pCellBody">ORDER</div><a name="wp81573"> </a><div class="pCellBody">SELECT</div><a name="wp81574"> </a><div class="pCellBody">SUM</div><a name="wp81575"> </a><div class="pCellBody">TRUE</div><a name="wp81576"> </a><div class="pCellBody">UNKNOWN</div><a name="wp81536"> </a><div class="pCellBody">WHERE</div></td></tr></table></div><p class="pBody"></p><a name="wp79968"> </a><p class="pBody">EJB QL keywords are also reserved words in SQL. In the future, the list of EJB QL keywords may expand to include other reserved SQL words. The Enterprise JavaBeans Specification recommends that you not use other reserved SQL words for EJB QL identifiers.</p><a name="wp79971"> </a><h4 class="pHeading3">Identification Variables</h4><a name="wp79972"> </a><p class="pBody">An <span style="font-style: italic">identification variable</span> is an identifier declared in the <code class="cCode">FROM</code> clause. Although the <code class="cCode">SELECT</code> and <code class="cCode">WHERE</code> clauses may reference identification variables, they cannot declare them. All identification variables must be declared in the <code class="cCode">FROM</code> clause.</p><a name="wp79973"> </a><p class="pBody">Since an identification variable is an identifier, it has the same naming conventions and restrictions as an identifier. For example, an identification variable is not case sensitive and it cannot be the same as an EJB QL keyword. (See the previous section for more naming rules.) Also, within a given EJB JAR file, an identifier name must not match the name of any entity bean or abstract schema. </p><a name="wp79976"> </a><p class="pBody">The <code class="cCode">FROM</code> clause may contain multiple declarations, separated by commas. A declaration may reference another identification variable that has been previously declared (to the left). In the following <code class="cCode">FROM</code> clause, the variable <code class="cCode">t</code> references the previously declared variable <code class="cCode">p</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">FROM Player p, IN (p.teams) AS t<a name="wp79977"> </a></pre></div><a name="wp79978"> </a><p class="pBody">Even if an identification variable is not used in the <code class="cCode">WHERE</code> clause, its declaration can affect the results of the query. For an example, compare the next two queries. The following query returns all players, whether or not they belong to a team:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT OBJECT(p)FROM Player p<a name="wp79980"> </a></pre></div><a name="wp79981"> </a><p class="pBody">In contrast, because the next query declares the <code class="cCode">t</code> identification variable, it fetches all players that belong to a team:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT OBJECT(p)FROM Player p, IN (p.teams) AS t<a name="wp79982"> </a></pre></div><a name="wp79983"> </a><p class="pBody">The following query returns the same results as the preceding query, but the <code class="cCode">WHERE</code> clause makes it easier to read:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT OBJECT(p)FROM Player pWHERE p.teams IS NOT EMPTY<a name="wp79984"> </a></pre></div><a name="wp79985"> </a><p class="pBody">An identification variable always designates a reference to a single value, whose type is that of the expression used in the declaration. There are two kinds of declarations: range variable and collection member.</p><a name="wp79987"> </a><h5 class="pHeading4">Range Variable Declarations</h5><a name="wp79989"> </a><p class="pBody">To declare an identification variable as an abstract schema type, you specify a range variable declaration. In other words, an identification variable can range over the abstract schema type of an entity bean. In the following example, an identification variable named <code class="cCode">p</code> represents the abstract schema named <code class="cCode">Player</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">FROM Player p<a name="wp79990"> </a></pre></div><a name="wp79991"> </a><p class="pBody">A range variable declaration may include the optional <code class="cCode">AS</code> operator:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">FROM Player AS p<a name="wp79992"> </a></pre></div><a name="wp79994"> </a><p class="pBody">In most cases, to obtain objects a query navigates through the relationships with path expressions. But for those objects that cannot be obtained by navigation, you can use a range variable declaration to designate a starting point (or <span style="font-style: italic">root</span>). </p><a name="wp79995"> </a><p class="pBody">If the query compares multiple values of the same abstract schema type, then the <code class="cCode">FROM</code> clause must declare multiple identification variables for the abstract schema: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">FROM Player p1, Player p2<a name="wp79996"> </a></pre></div><a name="wp80000"> </a><p class="pBody">For a sample of such a query, see <a  href="EJBQL4.html#wp79854">Example 9&nbsp;</a>.</p><a name="wp80003"> </a><h5 class="pHeading4">Collection Member Declarations</h5><a name="wp80004"> </a><p class="pBody">In a one-to-many relationship, the multiple side consists of a collection of entity beans. An identification variable may represent a member of this collection. To access a collection member, the path expression in the variable's declaration navigates through the relationships in the abstract schema. (For more information on path expressions, see the following section.) Because a path expression may be based on another path expression, the navigation can traverse several relationships. See <a  href="EJBQL4.html#wp79824">Example 6&nbsp;</a>.</p><a name="wp80008"> </a><p class="pBody">A collection member declaration must include the <code class="cCode">IN</code> operator, but it may omit the optional <code class="cCode">AS</code> operator.</p><a name="wp80009"> </a><p class="pBody">In the following example, the entity bean represented by the abstract schema named <code class="cCode">Player</code> has a relationship field called <code class="cCode">teams</code>. The identification variable called <code class="cCode">t</code> represents a single member of the <code class="cCode">teams</code> collection.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">FROM Player p, IN (p.te<code class="cCode">a</code>ms) AS t<a name="wp80010"> </a></pre></div><a name="wp80013"> </a><h3 class="pHeading2">Path Expressions</h3><a name="wp80014"> </a><p class="pBody">Path expressions are important constructs in the syntax of EJB QL, for several reasons. First, they define navigation paths through the relationships in the abstract schema. These path definitions affect both the scope and the results of a query. Second, they may appear in any of the three main clauses of an EJB QL query (<code class="cCode">SELECT</code>, <code class="cCode">WHERE</code>, <code class="cCode">FROM</code>). Finally, although much of EJB QL is a subset of SQL, path expressions are extensions not found in SQL.</p><a name="wp80015"> </a><h4 class="pHeading3">Syntax</h4><a name="wp81594"> </a><p class="pBody">Here is the syntax for path expressions:</p><a name="wp81106"> </a><p class="pBody">cmp_path_expression ::=<br />    {identification_variable |<br />     single_valued_cmr_path_expression}.cmp_field<br /><br />single_valued_cmr_path_expression ::=<br />    identification_variable.[single_valued_cmr_field.]*<br />     single_valued_cmr_field<br /><br />single_valued_path_expression ::=<br />    cmp_path_expression | single_valued_cmr_path_expression<br /><br />collection_valued_path_expression ::=<br />    identification_variable.[single_valued_cmr_field.]<br />    *collection_valued_cmr_field</p><a name="wp80021"> </a><p class="pBody">In the preceding diagram, the <code class="cCode">cmp_field</code> element represents a persistent field, and the <code class="cCode">cmr_field</code> element designates a relationship field. The term <code class="cCode">single_valued</code> qualifies the relationship field as the single side of a one-to-one or one-to-many relationship; the term <code class="cCode">collection_valued</code> designates it as the multiple (collection) side of a relationship. The <code class="cCode">single_valued_cmr_path_expression</code> is the abstract schema type of the related entity bean.</p><a name="wp80025"> </a><p class="pBody">The period (.) in a path expression serves two functions. If a period precedes a persistent field, it is a delimiter between the field and the identification variable. If a period precedes a relationship field, it is a navigation operator.</p><a name="wp80028"> </a><h4 class="pHeading3">Examples</h4><a name="wp80029"> </a><p class="pBody">In the following query, the <code class="cCode">WHERE</code> clause contains a <code class="cCode">cmp_path_expression</code>. The <code class="cCode">p</code> is an identification variable, and <code class="cCode">salary</code> is a persistent field of <code class="cCode">Player</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p)FROM Player p WHERE <span style="font-weight: bold">p.salary</span> BETWEEN ?1 AND ?2<a name="wp80030"> </a></pre></div><a name="wp80031"> </a><p class="pBody">The <code class="cCode">WHERE</code> clause of the next example also contains a <code class="cCode">cmp_path_expression</code>. The <code class="cCode">t</code> is an identification variable, <code class="cCode">league</code> is a single-valued relationship field, and <code class="cCode">sport</code> is a persistent field of <code class="cCode">league</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p)FROM Player p, IN (p.teams) AS t WHERE <span style="font-weight: bold">t.league.sport</span> = ?1<a name="wp80032"> </a></pre></div><a name="wp80033"> </a><p class="pBody">In the next query, the <code class="cCode">WHERE</code> clause contains a <code class="cCode">collection_valued_path_expression</code>. The <code class="cCode">p</code> is an identification variable, and <code class="cCode">teams</code> designates a collection-valued relationship field.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p)FROM Player p WHERE <span style="font-weight: bold">p.teams</span> IS EMPTY<a name="wp80034"> </a></pre></div><a name="wp80036"> </a><h4 class="pHeading3">Expression Types</h4><a name="wp80037"> </a><p class="pBody">The type of an expression is the type of the object represented by the ending element, which can be one of the following: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80038"> </a><div class="pSmartList1"><li>Persistent field </li></div><a name="wp80039"> </a><div class="pSmartList1"><li>Single-valued relationship field </li></div><a name="wp80040"> </a><div class="pSmartList1"><li>Collection-valued relationship field </li></div></ul></div><a name="wp80041"> </a><p class="pBody">For example, the type of the expression <code class="cCode">p.salary</code> is <code class="cCode">double</code> because the terminating persistent field (<code class="cCode">salary</code>) is a <code class="cCode">double</code>. </p><a name="wp80042"> </a><p class="pBody">In the expression <code class="cCode">p.teams</code>, the terminating element is a collection-valued relationship field (<code class="cCode">teams</code>). This expression's type is a collection of the abstract schema type named <code class="cCode">Team</code>. Because <code class="cCode">Team</code> is the abstract schema name for the <code class="cCode">TeamEJB</code> entity bean, this type maps to the bean's local interface, <code class="cCode">LocalTeam</code>. For more information on the type mapping of abstract schemas, see the section <a  href="EJBQL5.html#wp80440">Return Types</a>.</p><a name="wp80047"> </a><h4 class="pHeading3">Navigation</h4><a name="wp80048"> </a><p class="pBody">A path expression enables the query to navigate to related entity beans. The terminating elements of an expression determine whether navigation is allowed. If an expression contains a single-valued relationship field, the navigation may continue to an object that is related to the field. However, an expression cannot navigate beyond a persistent field or a collection-valued relationship field. For example, the expression <code class="cCode">p.teams.league.sport</code> is illegal, since <code class="cCode">teams</code> is a collection-valued relationship field. To reach the <code class="cCode">sport</code> field, the <code class="cCode">FROM</code> clause could define an identification variable named <code class="cCode">t</code> for the <code class="cCode">teams</code> field:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -