GARBA.ORG
EJB 2.0 Introduction > EJB-QL Index - Previous - Next

EJB-QL

An EJB-QL statement is delcared by appending a query block to the entity declaration. EJB-SQL queries may receive parameters by declaring them in the <method-params> section and then by refering to them using ?1, ?2, ?3, etc. according to the number of parameters.

    <entity>
        ......
        ......
        <query>
            <query-method>
                <method-name>findEmployeesBySalary</method-name>
                <method-params>
                    <method-param>java.lang.Integer</method-param>
                </method-params>
            </query-method>
            <ejb-ql>
                SELECT DISTINCT OBJECT(e) FROM employees e WHERE e.salary >= ?1
            </ejb-ql>
        </query>
    </entity>

The table name should be written as in the <abstract-schema-name> and fields as in <field-name>

Finder Methods

Finder methods are declared in the home class and always use the prefix "find". They are public and visible to the clients. As the default findByPrimaryKey() method implemented by the container, these methods may throw a FinderException.

In the Home Class:

   public Collection findEmployeesBySalary(Integer salary) throws FinderException;

Select Methods

Select methods are very much like finder methods but they are declared in the bean class and are private to the bean. They cannot be invoked by the client. Select methods must be declared abstract.

In ejb-jar.xml:

   <method-name>ejbSelectEmployeesBySalary</method-name>

In the Home Class:

   NOTHING!

In the Bean Class:

   public abstract Collection ejbSelectEmployeesBySalary(Integer salary) throws FinderException;

Examples

SELECT

  SELECT OBJECT (e) FROM employees e - Get a collection of all employee beans.
  SELECT e.name FROM employees e - Get as collection the name of all employees.
  SELECT OBJECT (e) FROM employees e WHERE e.cardNumber = 455433 - Get the employee who owns this credit card

THE IN OPERATOR

  SELECT DISTINCT OBJECT(b) FROM bosses b, IN (b.employees) e WHERE e.salary >= 500 - Select the bosses that have at least one employee that earns more than 500 bucks. The DISTINCT keyword ensures that the query does not return duplicates

ARITHMETIC OPERATORS

  SELECT OBJECT(e) FROM employees e WHERE (e.salary * 12) < 12000;

BETWEEN

  SELECT OBJECT(e) FROM employees e WHERE e.salary BETWEEN 6000 AND 8000;
  SELECT OBJECT(e) FROM employees e WHERE e.salary NOT BETWEEN 8000 AND 9000;

'IN' IN WHERE CLAUSE

  SELECT OBJECT(e) FROM employees e WHERE e.country IN ('de','es','it');

IS NULL

  SELECT OBJECT(e) FROM employees e WHERE e.wifeName IS NULL;
  SELECT OBJECT(e) FROM employees e WHERE e.wifeName IS NOT NULL;

IS EMPTY

  SELECT OBJECT(b) FROM bosses b WHERE b.employees IS EMPTY; - Checks for a boss without employees
  SELECT OBJECT(b) FROM bosses b WHERE b.employees IS NOT EMPTY; Checks for a boss with employees

Each boss has 0, 1 or more employees

Functional Expressions



© Ernesto Garbarino Top