📄 ch06s07.html
字号:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Declaring finders</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch06.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch06.html" title="Chapter 6. Customizing JAWS"><link rel="previous" href="ch06s04.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch06s04.html" title="Telling JAWS about your tables"><link rel="next" href="ch06s13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch06s13.html" title="Defining a type mapping"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch06.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch06.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch06s04.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch06s04.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch06s13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch06s13.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table><div class="section"><a name="d0e2215"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="d0e2215"></a>Declaring finders</h2></div></div><p>The finders to access your beans must all be declared in the home interface, according to the EJB specification.</p><div class="section"><a name="d0e2220"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2220"></a>Standard finders, automatically generated</h3></div></div><p>JAWS automatically generates the following finders for you:</p><div class="itemizedlist"><ul><li><p><a name="d0e2226"></a>findAll() will return a Collection of all the beans available</p></li><li><p><a name="d0e2229"></a>findByPrimaryKey(YourPK pk) will return a single bean with the corresponding primary key (the primary key class is defined in ejb-jar.xml)</p></li><li><p><a name="d0e2232"></a>for each of the cmp-fields of your bean, findByXX(YY fieldValue), where XX is the name of the cmp-field (NOT case-sensitive) and YY its class, will return a Collection of all the beans with the right value in this field.</p></li></ul></div><p>Note that these finders are only generated if you declare them in your home
interface. “Automatically generated” only means that JAWS will guess the meaning of your finder from its name, you don't have to add anything.</p></div><div class="section"><a name="d0e2240"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2240"></a>Custom finders</h3></div></div><p>JAWS then allows you to define customized finders. These finders must also be
declared in the home interface of your bean. You must then
provide additional information in jaws.xml about the query to be used for this
finder. This is done in a <finder> section in the section for
your entity bean. You have to specify the name of the finder (the same as in
the home interface), the WHERE part of the query, and the
ORDER part.
</p><p>Example: you want to select classes with a mininum number of students. Your
Class bean has the following structure:</p><pre class="programlisting">
<ejb-jar>
<display-name>Class</display-name>
<enterprise-beans>
<entity>
<description>Models a Class</description>
<ejb-name>ClassBean</ejb-name>
<home>org.jboss.docs.cmp.jaws.interfaces.ClassHome</home>
<remote>org.jboss.docs.cmp.jaws.interfaces.Class</remote>
<ejb-class>org.jboss.docs.cmp.jaws.bean.ClassBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-field><field-name>classId</field-name></cmp-field>
<cmp-field><field-name>teacherName</field-name></cmp-field>
<cmp-field><field-name>studentCount</field-name></cmp-field>
<primkey-field>classId</primkey-field>
</entity>
</enterprise-beans>
...
</ejb-jar>
</pre><p>You want to define a method in ClassHome:</p><pre class="programlisting">
public Collection findBigClasses(Integer minStudentCount, String teacher)
throws FinderException, RemoteException;
</pre><p>Your jaws.xml file will contain the following:</p><pre class="programlisting">
<jaws>
<enterprise-beans>
<entity>
<ejb-name>ClassBean</ejb-name>
...
<finder>
<name>findBigClasses</name>
<query>studentCount > {0} AND teacherName = {1}</query>
<order>studentCount DESC</order>
</finder>
</entity>
</enterprise-beans>
</jaws>
</pre><p>Then a call to findBigClasses(100, "Jones") will generate</p><pre class="programlisting">SELECT classId FROM ClassBean
WHERE studentCount > 100 AND teacherName = 'Jones'
ORDER BY studentCount DESC;</pre></div><div class="section"><a name="d0e2261"></a><div class="titlepage"><div><h3 class="title"><a name="d0e2261"></a>Custom finders with join queries</h3></div></div><p>Author:
<span class="author">Michel de Groot</span>
<tt><<a href="mailto:Michel.anke@wolmail.nl">Michel.anke@wolmail.nl</a>></tt> or <tt><<a href="mailto:rdgr@oce.nl">rdgr@oce.nl</a>></tt>
</p><p>By default, JAWS will use only the table for your bean to perform select queries. You can also use inner join queries on multiple tables with JAWS CMP for finder methods with EJB 1.1. This allows you to deliver powerful queries with minimal effort.</p><p>Standards compliance note: this feature is not standard or required in EJB 1.1. Therefore, EJBs that use this feature, might not be compile-free portable to other EJB containers.</p><p>You must use a database which has support for SQL92 (most databases do) or T-SQL join queries.</p><p>Example: </p><div class="itemizedlist"><ul><li><p><a name="d0e2287"></a>List your finder method in the EJB's home interface as usual.</p><pre class="programlisting">
public interface PopulationHome extends EJBHome {
/**
* Finds all populations of a specific type as defined in
* CreatureTypes of a Province.
* @param provinceId the primary key of the province
* @param type the type of creatures to find
*/
public Collection findByProvinceAndType(String provinceId, int type)
throws RemoteException, FinderException;
}
</pre></li><li><p><a name="d0e2292"></a>Declare your finder in jaws.xml, using the <query> tag. There are two options. The first presented here conforms to SQL92 and is the more generic and portable one. The second presented here is defined by T-SQL and is useful for MS Access 97 for example.</p><p>Generic SQL92 example:</p><pre class="programlisting">
<finder>
<name>findByProvinceAndType</name>
<query>,CreatureEJBTable
WHERE CreatureEJBTable.id = PopulationEJBTable.creatureId AND
provinceId={0} AND type={1}</query>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -