Tip

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.

    Requires Free Membership to View


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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.