Skip Headers

# Using EJB QL Finders

Previous

Next

EJB QL is the standard query language first defined in the EJB 2.0 specification. TopLink supports EJB QL. EJB QL finders let you specify an EJB QL string as the implementation of the query.

Note: Use a combination of an escape character and a double-quote ( \" ) when defining your query using EJB QL. For more information on correct query syntax, see a note at the end of "Configuring Named Query Selection Criteria".

EJB QL offers several advantages:

- It is the EJB 2.0 and 2.1 standard for queries.

- You can use it to construct most queries.

- You can implement dependent object queries with EJB QL.

The disadvantage of EJB QL is that it is difficult to use when you construct complex queries.

To create an EJB QL finder use this procedure:

1. Declare the finder on either the `LocalHome` or the `RemoteHome` interface.

2. Start TopLink Workbench.

3. Reimport the `ejb-jar.xml` file to synchronize the project to the file.

   TopLink Workbench synchronizes changes between the project and the `ejb-jar.xml` file.

The following is an example of a simple EJB QL query that requires one parameter. In this example, the question mark ( ? ) in`?1` specifies a parameter:

    SELECT OBJECT(employee) FROM Employee employee WHERE (employee.name =?1)

To create an EJB QL finder for an entity bean with container-managed persistence, use this procedure:

1. Declare the finder in the `ejb-jar.xml` file, and enter the EJB QL string in the `ejb-ql` tag.

2. Declare the finder on the `Home` interface, the `LocalHome` interface, or both, as required.

3. Start TopLink Workbench.

4. Specify the `ejb-jar.xml` file location and choose File > Updated Project from the `ejb-jar.xml` file to read in the finders.

5. Go to the Queries > Finders > Named Queries tab for the bean.

6. Add a finder, and give it the same name as the finder you declared on your bean's home. Then add any required parameters.

7. Select and configure the finder.

The following is an example of a simple EJB QL query that requires one parameter. In this example, the question mark ("?") in`?1` specifies a parameter.

    SELECT OBJECT(employee) FROM Employee employee WHERE (employee.name =?1)