Tutorial Index Page
Entity Bean "Finder" methods
If you went through the earlier tutorials and exercises, by now you must
be familiar with the findByPrimaryKey method. It has a relatively
obvious function, of retrieving an EJB given its primary key value.
While finding an EJB by its primary key is a useful function, there
are other ways to find EJBs also. These are known as its "finder"
methods.
The findByPrimaryKey will always return only one EJB instance,
because EJBs are uniquely identified by their primary keys. But this
is not true of other finders. E.g., in the "employee" EJB, a findBySalary
finder may match a number of EJBs for a given salary.
Therefore, a finder can have two types of signatures. It can return
a single EJB, in which case its return type will be the remote interface.
Alternatively, it can return a collection of EJBs, all of which matched
the search criteria.
If the finder found multiple beans but it is declared to return only
one, it will return one bean out of the collection it found.
If the finder is declared to return multiple beans, you can ask it to
return a Collection or an Enumeration.
To add a finder to an EJB, you are not supposed to write an implementation.
You only need to declare the method in the home interface. As of
this writing, the actual details of how the finder is implemented varies
from server to server (though in the EJB 2.0 specification which be released
later this year, this is more standardized.)
To see some finders, regenerate the "props" EJB, but this time, in the
Blizzard
step 4 , click at the "Generate Finders..." button.
Add finders where the "value" field equals a given value.
Look at the home interface generated, and you will see the new finder
signature. This finder can now be used to find a collection of EJBs
where "value" is euqal to the finder argument
Exercise:
-
Modify your client program so it can be given a "value" and will print
all keys that have that value (you will also need to rebuild the props
EJB, and restart the server or use the "hot"-redeployment feature),
-
Add "salary" finders to your employee EJB for "less than" values,
-
Write a test program to find all employees who earn less than a given amount.
|