Skip Headers

# Specifying a Custom EJB QL String in a DatabaseQuery

Previous

Next

All `DatabaseQuery` objects provide a `setEJBQLString` method that you can use to specify a custom EJB QL string.

For more information about using custom EJB QL in queries, see "Using EJB QL Calls".

Provide both a reference class and a `SELECT` clause, and execute the query in the usual manner.

EJB QL

    ReadAllQuery query = new ReadAllQuery(EmployeeBean.class);
    query.setEJBQLString("SELECT OBJECT(emp) FROM EmployeeBean emp");
    …
    Vector returnedObjects = (Vector)session.executeQuery(query);

Example: A Simple ReadAllQuery Using EJB QL and Passing Arguments defines the query similarly to Example: EJB QL, but creates, fills, and passes a vector of arguments to the `executeQuery` method.

A Simple ReadAllQuery Using EJB QL and Passing Arguments

    // First define the query
    ReadAllQuery query = new ReadAllQuery(EmployeeBean.class);
    query.setEJBQLString("SELECT OBJECT(emp) FROM EmployeeBean emp WHERE emp.firstName = ?1");
    query.addArgument("1", String.class);
    ...
    // Next define the arguments
    Vector arguments = new Vector();
    arguments.add("Bob");
    ...
    // Finally, execute the query passing in the arguments
    Vector returnedObjects = (Vector)session.executeQuery(query, arguments);