The EJB query language is a query specification language, similar to SQL, for entity beans with container-managed persistence. With it, the Bean Provider is able to specify the semantics of custom finder or EJB select methods in a portable way and in terms of the object model's entities, instead of the relational model's entities. This is possible because EJB QL is based on the abstract schema types of the entity beans.
Note: Both finder and EJB select methods are used to query the backend where the actual data is stored. The difference is that finder methods are accessible to the entity beans' clients, whereas select methods are internal to the implementation and not visible to clients.
An EJB QL query is a string consisting of the following clauses:
A SELECT clause, which determines the type of the objects or values to be selected.
A FROM clause, which provides declarations that designate the domain to which the specified expressions apply.
An optional WHERE clause, which may be used to restrict the results that are returned by the query.
An optional ORDER BY clause, which may be used to order the results that are returned by the query.
The result type can be an EJBLocalObject, an EJBObject, a CMP-field value, a collection of any of these types, or the result of an aggregate function.
EJB QL queries are defined by the Bean Provider in the deployment descriptor. The SQL statements for the actual database access is generated automatically by the deployment tooling. As an example, this query retrieves customers that have accounts with a large balance:
SELECT OBJECT(c) FROM Customer c, IN(c.accounts) a WHERE a.balance > ?1
As you can see, this EJB QL statement is independent of the database implementation. It follows a CMR relationship from customer to account and queries the account balance. Finder and EJB select methods specified using EJB QL are portable to any EJB 2.1 environment.
Note: These finder and EJB select methods may also be portable to EJB 2.0 environments if they do not use the new order and aggregate features defined by the EJB 2.1 specification.
Finder and Select Methods
Finder and select methods use EJB QL queries to return objects and state information of entity beans using container-managed persistence.
A select method is similar to a finder method in the following ways:
A select method can return a local or remote interface (or a collection of interfaces).
A select method queries a database.
The deployment descriptor specifies an EJB QL query for a select method.
The entity bean class does not implement the select method.
However, a select method differs significantly from a finder method:
A select method can return a persistent field (or a collection thereof) of a related entity bean. A finder method can return only a local or remote interface (or a collection of interfaces).
Because it is NOT exposed in any of the local or remote interfaces, a select method CANNOT be invoked by a client. It can be invoked only by the methods implemented within the entity bean class. A select method is usually invoked by either a business or a home method.
A select method is defined in the entity bean class. For bean-managed persistence, a finder method is defined in the entity bean class, but for container-managed persistence it is not.
The signature for a select method must follow these rules:
The prefix of the method name must be ejbSelect.
The access control modifier must be public.
The method must be declared as abstract.
The throws clause must include the javax.ejb.FinderException.