Skip Headers

# Using Named Queries

Previous

Next

Named queries improve application performance because they are prepared once and they (and all their associated supporting objects) can be efficiently reused thereafter making them well suited for frequently executed operations.

You can configure named queries at the session (see "Configuring Named Queries at the Session Level") or descriptor (see "Configuring Named Queries at the Descriptor Level") level.

For a session-level named query, you can execute the query using any of the following `Session` API calls:

- `executeQuery(String queryName)`

- `executeQuery(String queryName, arg1)`

- `executeQuery(String queryName, arg1, arg2)`

- `executeQuery(String queryName, arg1, arg2, arg3)`

- `executeQuery(String queryName, Vector args)`

Executing a Session-Level Named Query

    Vector args = new Vector();
    args.add("Sarah");
    Employee sarah = (Employee)session.executeQuery(
        "employeeReadByFirstName",
        args
    );

For a descriptor-level named query, you can execute the query using any of the following `Session` API calls, as Example: Executing a Descriptor Level Named Query shows:

- `executeQuery(String queryName, Class domainClass)`

- `executeQuery(String queryName, Class domainClass, arg1)`

- `executeQuery(String queryName, Class domainClass, arg1, arg2)`

- `executeQuery(String queryName, Class domainClass, arg1, arg2, arg3)`

- `executeQuery(String queryName, Class domainClass, Vector args)`

Executing a Descriptor Level Named Query

    Vector args = new Vector();
    args.add("Sarah");
    Employee sarah = (Employee)session.executeQuery(
        "ReadByFirstName",
        Employee.class,
        args
    );

For more information, see "Named Queries"