Skip to main content

IBM WebSphere Developer Technical Journal: Dynamic EJB Finders

Tim Biernat, principal, softwareMentor.com
Photo: Tim Biernat
Tim Biernat has held various research and development positions at General Dynamics and Motorola, and has taught internationally for IBM and Learning Tree International. Tim was trained as an electrical engineer at Marquette University and the University of Texas, and his professional experience spans 17 years, including research in fault tolerant real-time avionics software, and development of many distributed object business systems. Currently, Tim is principal of softwareMentor.com, a distributed object technology consulting firm based in Milwaukee.

Summary:  This article describes the solution to another challenge posed by a Customer Relationship Management (CRM) application, an application that enables the business to manage contact and relationship information for customers and prospects, that is, using EJBs to enable flexible customer queries.

Date:  20 Nov 2001
Level:  Intermediate
Activity:  186 views

Requirements

Recently, I worked on a Customer Relationship Management (CRM) application that enables the business to manage contact and relationship information for customers and prospects. A solution for persisting session state was discussed in my previous article, Persisting Session State: CRM Recent Customer List. This article describes the solution to another challenge posed by this CRM application: using EJBs to enable flexible customer queries.

One of the application requirements is customer search, enabling the user to enter one or more criteria to generate a filtered list of customers. This CRM application accesses the customer database via EJBs, to enforce transactional integrity and employ a layered architecture. The goal for this requirement is to enable flexible queries against the customer database within this EJB architecture. You can apply this approach to a wide range of problems involving dynamic EJB queries. The solution was developed using VisualAge® for JavaTM, Enterprise Edition, Version 3.5.3, and WebSphere® Application Server, Advanced Edition, Version 3.5.3.


Background

WebSphere Advanced Edition supports three different techniques for EJB "custom" finders: select string, where clause, and method-based.

  • select string custom finder
    Lets you specify a static SQL SELECT string in the <beanClassName>FinderHelper interface. This interface gives the EJB container "hints" on how to implement the query. While this approach is straightforward, it is not recommended for new WebSphere development. Here is an example of select clause specification:

    public static final String findGreaterThanQueryString = "SELECT * FROM MYTABLE T1 WHERE T1.VALUE > ?";

  • where clause custom finder
    Requires only that you specify the WHERE clause of the SQL query string in the <beanClassName>FinderHelper interface. This is the recommended approach for simple EJB queries, as in the following example:

    public static final String findGreaterThanWhereClause = "T1.VALUE > ?";

  • method custom finder
    Enables you to dynamically construct a java.sql.PreparedStatement for container queries. You must create a class in the same package as the EJB deployed code and name it <beanClassName>FinderObject. The class must extend com.ibm.vap.finders.VapEJSJDBCFinderObject and must implement the <beanClassName>FinderHelper interface. Then you can implement a method to generate the required PreparedStatement, using a method signature as follows:

    public java.sql.PreparedStatement findGreaterThan(int threshold) throws Exception;

We can design a solution based on the method custom finder approach that is much easier to implement and maintain than either of the static finder approaches, which require many different static finders to cover all scenarios. This article describes how to take advantage of this method custom finder to assemble complex, dynamic queries against EJB data.


Design

The method custom finder approach lets you build complex queries on the fly. Helper classes make statement construction easier. See Figure 1 below.


Figure 1.

The SearchCriteria class is just a bean that encapsulates the information required to construct a single SQL WHERE clause: field (column name), operator (<, =, >, etc.) and criteria (represented as an Object; String, Integer, Long, Date, and Timestamp currently supported). The SearchCriteriaHelper does the real work of taking one or more SearchCriteria, and constructing an appropriate PreparedStatement using the following two methods.

public String getWhereClause() { 
 
        StringBuffer where = new StringBuffer(); 
 
        for (int i=0; i < searchCriteria.length; i++) { 
                SearchCriteria sc = searchCriteria[i]; 
                if (i > 0) where.append(" AND "); 
                where.append(sc.getField()).append(" 
                 ").append(sc.getOperator()).append(" ?"); 
        } 
         
        return where.toString(); 
} 
 
public void injectParams(PreparedStatement ps)  
        throws SQLException, SearchCriteriaException { 
         
        // inject parameters based on type 
        for (int i=0; i < searchCriteria.length; i++) { 
                SearchCriteria sc = searchCriteria[i]; 
                Object o = sc.getCriteria(); 
                if (o instanceof String) { 
                        ps.setString(i+1, (String)sc.getCriteria()); 
                } else if (o instanceof Integer) { 
                        ps.setInt(i+1, ((Integer)o).intValue()); 
                } else if (o instanceof Long) { 
                        ps.setLong(i+1, ((Long)o).longValue()); 
                } else if (o instanceof java.sql.Date) { 
                        ps.setDate(i+1, ((java.sql.Date)o)); 
                } else if (o instanceof java.sql.Timestamp) { 
                        ps.setTimestamp(i+1, ((java.sql.Timestamp)o)); 
                } else { 
                        throw new SearchCriteriaException("Criteria 
                         type not recognized."); 
                } 
        } 
}

Using these helper classes, we create a suitable FinderObject, and update the FinderHelper and Home interfaces to support searching with one or more SearchCriteria. See Figure 2 below.


Figure 2.
Figure 2

Now it is a simple matter to implement the FinderObject's findBySearchCriteria method.

public java.sql.PreparedStatement 
 findBySearchCriteria(SearchCriteria[] criteria)  
        throws Exception { 
         
        SearchCriteriaHelper helper = 
         new SearchCriteriaHelper(criteria); 
        PreparedStatement ps = 
         getMergedPreparedStatement(helper.getWhereClause()); 
        helper.injectParams(ps); 
  
        return ps; 
}


Implementation

A simplified but fully functional version of the CRM customer search design implementation is available for download. This includes the CustomerEJB and associated code, database schemas, and sample data.


Validation

It is easy to test your EJB Finder in VisualAge for Java using the EJB Test Client. The Test Client relies on Java reflection, and is a powerful tool for unit testing EJBs. The key to using it is to remember to right-click to invoke methods and construct objects. See Figure 3 below.


Figure 3.

Set up one of more search criteria by instantiating the proper objects, then invoke the finder and inspect the returned remote objects. JDBC exceptions will be returned for bogus field names and operators. For more information on using the EJB Test Client, see the VisualAge for Java online help documentation.


Summary

I hope that you have found these techniques useful additions to your bag of development tricks.

Top of page



Download

NameSizeDownload method
finder.zip29 KBFTP|HTTP

Information about download methods


About the author

Photo: Tim Biernat

Tim Biernat has held various research and development positions at General Dynamics and Motorola, and has taught internationally for IBM and Learning Tree International. Tim was trained as an electrical engineer at Marquette University and the University of Texas, and his professional experience spans 17 years, including research in fault tolerant real-time avionics software, and development of many distributed object business systems. Currently, Tim is principal of softwareMentor.com, a distributed object technology consulting firm based in Milwaukee.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=23581
ArticleTitle=IBM WebSphere Developer Technical Journal: Dynamic EJB Finders
publish-date=11202001
author1-email=
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers