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

📄 ejbql4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>Example Queries</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="EJBQL3.html" />    <link rel="Next" href="EJBQL5.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="EJBQL3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="EJBQL5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp79755"> </a><h2 class="pHeading1">Example Queries</h2><a name="wp79756"> </a><p class="pBody">The following queries are from the <code class="cCode">PlayerEJB</code> entity bean of the <code class="cCode">RosterApp</code> J2EE application, which is documented in Chapter&nbsp;<a  href="CMP.html#wp79663">22</a><a  href="CMP.html#wp80501"></a>. To see the relationships between the beans of the <code class="cCode">RosterApp</code>, see <a  href="CMP2.html#wp79760">Figure 22-1</a>.</p><a name="wp79767"> </a><h3 class="pHeading2">Simple Finder Queries</h3><a name="wp79768"> </a><p class="pBody">If you are unfamiliar with EJB QL, these simple queries are a good place to start. </p><a name="wp79770"> </a><div style="color: #000000; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: bold; margin-bottom: 4pt; margin-left: 24pt; margin-right: 0pt; margin-top: 13pt; text-align: left; text-decoration: none; text-indent: 0pt; text-transform: none">Example 1&nbsp;&nbsp;</div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT OBJECT(p)FROM Player p<a name="wp79771"> </a></pre></div><a name="wp79772"> </a><p class="pBody"><span style="font-style: italic">Data retrieved:</span> All players.</p><a name="wp79773"> </a><p class="pBody"><span style="font-style: italic">Finder method:</span> <code class="cCode">findall()</code></p><a name="wp79774"> </a><p class="pBody"><span style="font-style: italic">Description:</span> The <code class="cCode">FROM</code> clause declares an identification variable named <code class="cCode">p</code>, omitting the optional keyword <code class="cCode">AS</code>. If the <code class="cCode">AS</code> keyword were included, the clause would be written as follows: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">FROM Player AS p<a name="wp79775"> </a></pre></div><a name="wp79776"> </a><p class="pBody">The <code class="cCode">Player</code> element is the abstract schema name of the <code class="cCode">PlayerEJB</code> entity bean. Because the bean defines the <code class="cCode">findall</code> method in the <code class="cCode">LocalPlayerHome</code> interface, the objects returned by the query have the <code class="cCode">LocalPlayer</code> type. </p><a name="wp79780"> </a><p class="pBody"><span style="font-style: italic">See also:</span> <a  href="EJBQL5.html#wp79971">Identification Variables</a></p><a name="wp79781"> </a><div style="color: #000000; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: bold; margin-bottom: 4pt; margin-left: 0pt; margin-right: 0pt; margin-top: 13pt; text-align: left; text-decoration: none; text-indent: 0pt; text-transform: none">Example 2&nbsp;&nbsp;</div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT <span style="font-weight: bold">OBJECT</span>(p)FROM Player pWHERE p.position = ?1<a name="wp79782"> </a></pre></div><a name="wp79783"> </a><p class="pBody"><span style="font-style: italic">Data retrieved:</span> The players with the position specified by the finder method's parameter.</p><a name="wp79785"> </a><p class="pBody"><span style="font-style: italic">Finder method:</span><code class="cCode"> findByPosition(String position)</code></p><a name="wp79786"> </a><p class="pBody"><span style="font-style: italic">Description:</span> In a <code class="cCode">SELECT</code> clause, the <code class="cCode">OBJECT</code> keyword must precede a stand-alone identification variable such as <code class="cCode">p</code>. (A stand-alone identification variable is not part of a path expression.) The <code class="cCode">DISTINCT</code> keyword eliminates duplicate values.</p><a name="wp79787"> </a><p class="pBody">The <code class="cCode">WHERE</code> clause restricts the players retrieved by checking their <code class="cCode">position</code>, a persistent field of the <code class="cCode">PlayerEJB</code> entity bean. The <code class="cCode">?1</code> element denotes the input parameter of the <code class="cCode">findByPosition</code> method.</p><a name="wp79793"> </a><p class="pBody"><span style="font-style: italic">See also:</span> <a  href="EJBQL5.html#wp80071">Input Parameters</a>, <a  href="EJBQL5.html#wp80462">DISTINCT and OBJECT Keywords</a></p><a name="wp79795"> </a><div style="color: #000000; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: bold; margin-bottom: 4pt; margin-left: 0pt; margin-right: 0pt; margin-top: 13pt; text-align: left; text-decoration: none; text-indent: 0pt; text-transform: none">Example 3&nbsp;&nbsp;</div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p)FROM Player pWHERE p.position = ?1 AND p.name = ?2<a name="wp79796"> </a></pre></div><a name="wp79797"> </a><p class="pBody"><span style="font-style: italic">Data retrieved:</span> The players with the specified position and name.</p><a name="wp79798"> </a><p class="pBody"><span style="font-style: italic">Finder method:</span> <code class="cCode">findByPositionAndName(String position, String name)</code></p><a name="wp79799"> </a><p class="pBody"><span style="font-style: italic">Description:</span> The <code class="cCode">position</code> and <code class="cCode">name</code> elements are persistent fields of the <code class="cCode">PlayerEJB</code> entity bean. The <code class="cCode">WHERE</code> clause compares the values of these fields with the parameters of the <code class="cCode">findByPositionAndName</code> method. EJB QL denotes an input parameter with a question mark followed by an integer. The first input parameter is <code class="cCode">?1</code>, the second is <code class="cCode">?2</code>, and so forth.</p><a name="wp79800"> </a><h3 class="pHeading2">Finder Queries That Navigate to Related Beans</h3><a name="wp79802"> </a><p class="pBody">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. </p><a name="wp79805"> </a><div style="color: #000000; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: bold; margin-bottom: 4pt; margin-left: 0pt; margin-right: 0pt; margin-top: 13pt; text-align: left; text-decoration: none; text-indent: 0pt; text-transform: none">Example 4&nbsp;&nbsp;</div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p)FROM Player p, IN (p.teams) AS tWHERE t.city = ?1<a name="wp79806"> </a></pre></div><a name="wp79807"> </a><p class="pBody"><span style="font-style: italic">Data retrieved:</span> The players whose teams belong to the specified city.</p><a name="wp79808"> </a><p class="pBody"><span style="font-style: italic">Finder method:</span> <code class="cCode">findByCity(String city)</code></p><a name="wp79809"> </a><p class="pBody"><span style="font-style: italic">Description:</span> The <code class="cCode">FROM</code> clause declares two identification variables: <code class="cCode">p</code> and <code class="cCode">t</code>. The <code class="cCode">p</code> variable represents the <code class="cCode">PlayerEJB</code> entity bean, and the <code class="cCode">t</code> variable represents the related <code class="cCode">TeamEJB</code> beans. The declaration for <code class="cCode">t</code> references the previously declared <code class="cCode">p</code> variable. The <code class="cCode">IN</code> keyword signifies that <code class="cCode">teams</code> is a collection of related beans. The <code class="cCode">p.teams</code> expression navigates from a <code class="cCode">PlayerEJB</code> bean to its related <code class="cCode">TeamEJB</code> beans. The period in the <code class="cCode">p.teams</code> expression is the navigation operator.</p><a name="wp79811"> </a><p class="pBody">In the <code class="cCode">WHERE</code> clause, the period preceding the persistent variable <code class="cCode">city </code>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.</p><a name="wp79812"> </a><p class="pBody">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 <code class="cCode">teams</code> field is a collection, the <code class="cCode">WHERE</code> clause cannot specify<code class="cCode"> p.teams.city</code>--an illegal expression. </p><a name="wp79816"> </a><p class="pBody"><span style="font-style: italic">See also:</span> <a  href="EJBQL5.html#wp80013">Path Expressions</a></p><a name="wp79817"> </a><div style="color: #000000; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: bold; margin-bottom: 4pt; margin-left: 0pt; margin-right: 0pt; margin-top: 13pt; text-align: left; text-decoration: none; text-indent: 0pt; text-transform: none">Example 5&nbsp;&nbsp;</div><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SELECT DISTINCT OBJECT(p)FROM Player p, IN (p.teams) AS tWHERE t.league = ?1<a name="wp79818"> </a></pre></div><a name="wp79819"> </a><p class="pBody"><span style="font-style: italic">Data retrieved:</span> The players that belong to the specified league.</p><a name="wp79820"> </a><p class="pBody"><span style="font-style: italic">Finder method:</span> <code class="cCode">findByLeague(LocalLeague league)</code></p><a name="wp79821"> </a><p class="pBody"><span style="font-style: italic">Description:</span> The expressions in this query navigate over two relationships. The <code class="cCode">p.teams</code> expression navigates the <code class="cCode">PlayerEJB</code>-<code class="cCode">TeamEJB</code> relationship, and the <code class="cCode">t.league</code> expression navigates the <code class="cCode">TeamEJB-LeagueEJB</code> relationship. </p>

⌨️ 快捷键说明

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