Adding finder methods to EJB 2.x beans using EJB QL

You can add "find" or "ejbSelect" finder methods to the home interfaces of EJB 2.x CMP beans. The finder methods use the EJB query language. A wizard guides you through the steps for adding new or existing finders to your bean.

The EJB query language (EJB QL) syntax is based on SQL and allows searches on the persistent attributes of an enterprise bean and associated bean attributes. The query language is independent of the bean's mapping to a relational datastore and it is portable. The query language is compiled into SQL at deployment time based on the schema mapping for the bean.

For details on EJB QL, go to the WebSphere® Application Server Information Center and search for the keywords "EJB QL".

To add an EJB QL query to an EJB 2.x enterprise bean:

  1. In the Project Explorer view of the J2EE perspective, right-click the Deployment Descriptor for your EJB project and select Open With > Deployment Descriptor Editor to open the deployment descriptor editor.
  2. On the Bean page of the editor, select the EJB 2.x CMP entity bean that you want to add a finder method to.
  3. Scroll to the Queries section, and click Add. The Add Finder Descriptor wizard opens.
  4. Select one of the following options:
    • New - creates a new finder method.
    • Existing - uses an existing finder method.
  5. Select a method type from the following choices:
    • find Method - Finder methods are defined in the home interfaces (local, remote, or both) of entity beans. The return and result of the finder method depends on where it defined.
    • ejbSelect method - ejbSelect methods are special query methods not directly exposed through the client view. ejbSelect methods are typically used to select the persistent state of an entity, or to select entities that are related to the entity bean for which the query is defined.
  6. If you are using an existing finder, select from a list of finders that were discovered, then click Finish.
  7. If you are defining a new finder, complete the following steps.
  8. For the type of finder, select at least one of the following check boxes:
    • Local - Select to add the method declaration to the local home interface for local method calls.
    • Remote - Select to add the method declaration to the remote home interface for remote method calls.
    You can select both check boxes to include the finder in both remote and local interfaces. The check box will only be available if your bean includes the respective interface.
  9. Optional: If you are creating an ejbSelect method, you can select the Return remote entities check box. This option indicates that you want remote references returned. This option is only available with this method type.
  10. In the Name field, type a name for the new query. Query method names must begin with the letters indicating the type selected, for example find or ejbSelect.
  11. Optional: Click Add to add a method parameter.
  12. In the Return type drop-down list, select the type that the finder method will return.
  13. Click Next. The Bean, Abstract schema name, and Query method name fields are read-only.
  14. Optional: Type a description for your method in the Description text area.
  15. Optional: To help you generate your EJB query, you can start with a sample query in the Select a sample query drop down list.
  16. Type or modify your query in the Query statement text area.
  17. Click Finish.
A <query></query> element is added to the deployment descriptor for the bean. The actual query is added to the <ejb-ql></ejb-ql> element. The method declaration is added to the home interface (local, remote, or both, depending on your selections).
To edit or remove the query, select the query in the Queries section and click Edit or Remove.
Related concepts
Finder methods for CMP entity beans

Feedback