ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Learning EJB QL

by Jeelani B. Shaik
09/19/2001

Why an EJB Query Language?
The specification for Enterprise Java Beans 1.1 doesn't offer a standard way to define queries for finder methods in entity beans with container-managed persistence (CMP). So EJB container providers have defined their own query syntax for the finder methods of entity beans. For example, BEA's WebLogic Application Server v. 5.1 defines a query language called WLQL.

Because of this, developers have to redefine queries for the finder methods whenever an application is moved from one vendor's application server to another's. Obviously, this makes applications built using CMP less portable.

What's more, EJB 1.1 offered no standard way to define a query for an entity bean with CMP to navigate with other entity beans in a variety of code contexts, relationships, or associations. There was no proper mechanism to define queries to navigate from one entity bean to its dependent classes and the member variables of those dependent classes.

EJB 2.0 deals with these shortcomings by defining the EJB Query Language. EJB QL is based on the SQL-92 specification for defining various finder and select methods of entity beans with CMP. The EJB QL query string consists of three clauses: SELECT, FROM, and WHERE. Among other things, EJB QL offers a standard way to define relationships between entity beans and dependent classes by introducing abstract schema types and relationships in the deployment descriptor. EJB QL also defines queries for navigation using abstract schema names and relationships.

What is EJB QL?

The following syntax is the basic format of the EJB QL query:

EJB QL ::=  select_clause from_clause  [ where_clause]

The SELECT clause was an optional clause in the earlier EJB 2.0 proposed final draft (released on 23rd October, 2000). In the final draft released on 24th April 2001, the SELECT clause is made mandatory.

The EJB QL must always contain SELECT and FROM clauses. The WHERE clause is optional (see sidebar). The FROM clause provides declarations for the identification variables based on abstract schema name, for navigating through the schema. The SELECT clause uses these identification variables to define the return type of the query, and the WHERE clause defines the conditional query.

The query for EJB QL is defined in the deployment descriptor using the <query> tag as shown below:

<query>
	<query-method>
		<method-name></method-name>
<method-params> 
<method-param></method-param>
		</method-params>	
	</query-method>
<result-type-mapping></result-type-mapping>
	<ejb-ql></ejb-ql>
</query>

You specify the name of the finder or select method in <method-name> and the parameters in <method-param>. The <result-type-mapping> indicates the return type, and can contain either Local (the default) or Remote values. The query string is in the <ejb-ql> tag.

I will give a brief description of the finder and select methods since these methods use EJB QL to define the queries. EJB 2.0 defines the finder and select methods for entity beans. The select method is a new addition to the specification.

Finder Methods: Finder methods get either a single or a collection of entity bean instances from the persistence store through a relational database. These methods define the home interface(s) of an entity bean. Hence, they are exposed to the client. A home interface can either be a Remote Home interface, EJBHome, or a Local Home interface, EJBLocalHome. The return type of the finder method defined in the remote home interface is either the entity bean's remote interface or a collection of objects implementing the entity bean's remote interface. The return type of the finder method defined in the local home interface is either the entity bean's local interface or a collection of objects implementing the entity bean's local interface. For example:

// Remote Home Interface
public interface OrderHome extends javax.ejb.EJBHome {
    ...
  public Order findByPrimaryKey(int orderId) throws FinderException,RemoteException;
  public Order findByBiggestOrder() throws FinderException,RemoteException;
public java.util.Collection findAllOrders(String supplierName) throws FinderException,RemoteException;
}
// Local Home Interface
public interface OrderHome extends javax.ejb.EJBLocalHome {
    ...
  public Order findByPrimaryKey(int orderId) throws FinderException;
  public Order findBiggestOrder() throws FinderException;
public java.util.Collection findAllOrders(String supplierName) throws FinderException;
}

Comment on this articleShare your experiences with this new database-driven component architecture for Java.
Post your comments

Select Methods: Select methods exist in the entity bean as a special type of query method. They are not declared in the home interface; therefore, they are not exposed to the client. The finder methods are not useful to access cmp-field, or any remote interface instance defined in cmr-field. Using select methods, an entity bean returns an instance of cmp-field type, or the remote interfaces represented by the cmr-field.

Select methods are usually two types.

  • ejbSelect<METHOD>
  • ejbSelect<METHOD>InEntity

Example:

public abstract class OrderBean implements javax.ejb.EntityBean {
...
public abstract java.util.Collection ejbSelectAllOrderedProducts(Date date)
throws FinderException;
...
public abstract java.util.Collection ejbSelectAllOrderedProductsInEntity(Date date)
throws FinderException;
}

The ejbSelect<METHOD> is not associated with a particular instance of the entity bean. So in the above example, the ejbSelectAllOrderProducts returns a collection of all products associated with all orders. Therefore, the ejbSelect<METHOD>InEntity is specific to the entity instance executed. The above ejbSelectAllOrderProductsInEntity returns all of the products associated with that instance of OrderBean.

Here the Order example explains each clause in detail. The relationship between OrderEJB, LineItemEJB, ProductEJB and AddressEJB are shown in the following diagram:

Diagram.

The relationships, as defined in the deployment descriptor, are shown below:

<relationships>
<!--
ONE-TO-MANY: Order LineItem
-->

<ejb-relation>
  <ejb-relation-name>Order-LineItem</ejb-relation-name>
  <ejb-relationship-role>
    <ejb-relationship-role-name>
    order-has-lineitems
    </ejb-relationship-role-name>
    <multiplicity>One</multiplicity>
    <relationship-role-source>
      <ejb-name>OrderEJB</ejb-name>
    </relationship-role-source>
    <cmr-field>
      <cmr-field-name>lineItems</cmr-field-name>
      <cmr-field-type>java.util.Collection
      </cmr-field-type>
    </cmr-field>
  </ejb-relationship-role>

  <ejb-relationship-role>
    <ejb-relationship-role-name>lineitem-belongsto-order
    </ejb-relationship-role-name>
    <multiplicity>Many</multiplicity>
    <cascade-delete/>
    <relationship-role-source>
      <ejb-name>LineItemEJB</ejb-name>
    </relationship-role-source>
    <cmr-field>
      <cmr-field-name>order</cmr-field-name>
    </cmr-field>
  </ejb-relationship-role>
</ejb-relation>
<!--
ONE-TO-MANY unidirectional relationship:
Product is not aware of its relationship with LineItem
-->
<ejb-relation>
  <ejb-relation-name>Product-LineItem</ejb-relation-name>
  <ejb-relationship-role>
    <ejb-relationship-role-name>product-has-lineitems</ejb-relationship-role-name>
    <multiplicity>One</multiplicity>
    <relationship-role-source>
      <ejb-name>ProductEJB</ejb-name>
    </relationship-role-source>
    <!-- since Product does not know about LineItem there is no cmr field in Product for accessing Lineitem -->
  </ejb-relationship-role>
  <ejb-relationship-role>
    <ejb-relationship-role-name>lineitem-for-product</ejb-relationship-role-name>
    <multiplicity>Many</multiplicity>
    <relationship-role-source>
      <ejb-name>LineItemEJB</ejb-name>
    </relationship-role-source>
    <cmr-field>
      <cmr-field-name>product</cmr-field-name>
    </cmr-field>
  </ejb-relationship-role>
</ejb-relation>
</relationships>

Pages: 1, 2, 3

Next Pagearrow