The EJB Query Language (EJBQL)

The EJB Query Language (EJBQL)
Mark Wutka

Mark is the author of Special Edition Using Java Server Pages and Servlets (2000, Que). This tip is excerpted from InformIT.


You might have noticed that different EJB container vendors have different ways of defining the queries for finder methods. Some require you to specify an SQL query, while others have their own query languages. EJB 2.0 introduces a standard EJB query language so that you need to write your finder queries only one time. In fact, you now include your finder queries in the ejb-jar.xml deployment descriptor file instead of the vendor-specific deployment descriptor.

EJBQL is similar to SQL, so it's very easy to pick up. Basically, you define a WHERE clause and use ?1, ?2, ?3, and so on to represent the parameters for your finder method. For example, a finder method called findByLastName(String aLastName) might be defined with a query like this:

WHERE lastName = ?1

In your deployment descriptor, you define it this way:

<query>
    <query-method>
        <method-name>findByLastName</method-name>
        <method-params>
            <method-param>java.lang.String</method-param>
        </method-params>
    </query-method>
    <ejb-ql>
        <![CDATA[WHERE lastName like ?1]]>
    </ejb-ql>

</query>

As with SQL, you can use AND and OR to compare multiple fields, like this:

WHERE firstName = ?1 and lastName=?2

EJBQL has quite a few variations for more complex queries. You can locate objects based on values in related object and other interesting variations. The best part of EJBQL, other than the fact that it is so similar to SQL, is that it is standard across all EJB 2.0-compliant containers. You need to write the queries only once.


To read more of this tip, click over to InformIT. You have to register there, but registration is free.

Did you like this tip? Why not let us know. Email your comments, or go to our tips page and submit one of your own.

Related Book

JavaServer Pages
Author : Hans Bergsten
Publisher : O'Reilly & Associates
ISBN/CODE : 156592746X
Cover Type : Soft Cover
Pages : 572
Published : Dec 2000
Summary :
JavaServer Pages (JSP) technology provides an easy way to create dynamic web pages. JSP uses a component-based approach that allows web developers to easily combine static HTML for look-and-feel with Java components for dynamic features. The simplicity of this component-based model, combined with the cross-platform power of Java, allows a web development environment with enormous potential.


This was first published in February 2001

Dig deeper on Windows Operating System Management

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close