persistence@glassfish.java.net

Is using Toplink ExpressionBuilder a good choice

From: sonavor <jw_at_worleytown.com>
Date: Thu, 24 Jul 2008 22:48:16 -0700 (PDT)

I pretty new to JPA and using Toplink. Here is an what I have tried in using
JPA and Toplink to create a dynamic query.

My environment is a Swing application using JPA with a Derby database that
the Swing application accesses using the embedded Derby driver. I am
compiling and running under Java 1.6.

In the example below I am using a JPA entity class called Person. I also
use the Person class to populate with person search values from my search
entry form. The Person object with the search values is named srchPerson
and is passed to a method that performs the person search. The search entry
fields are all optional so the query method must dynamically build the JPA
query.

I decided to try the Toplink ExpressionBuilder to dynamically build
expressions and append them together like this:

Expression exp = null;
Expression lastNameExp = null;
Expression memberExp = null;
ExpressionBuilder eb = new ExpressionBuilder(Person.class);

in my case I pass in a Person class with fields populated that are to be
used in the search criteria so I test them for values and build up the main
Expression:

if ( srchPerson.getLastName() != null && srchPerson.getLastName().length() >
0 ) {
    lastNameExp =
eb.get("lastName").likeIgnoreCase(srchPerson.getLastName()+"%");
    exp = lastNameExp;
}

if ( srchPerson.getMember() != null && srchPerson.getMember().length() > 0 )
{
    if ( srchPerson.getMember().equals("N")) {
        memberExp = eb.get("member").notEqual("Y");
    } else {
        memberExp = eb.get("member").equal("Y");
    }
    if ( exp != null ) {
        exp = exp.and(memberExp);
    } else {
        exp = memberExp;
    }
}

Those are just two examples of my dynamic expression building logic. At the
end I use the main Expression: exp to get a Query object from my
EntityManager:

if ( exp != null ) {
    Query q = ( (oracle.toplink.essentials.ejb.cmp3.EntityManager)
em.getDelegate()).createQuery(exp, Person.class);
}

Later I test the Query for null then set parameters from my user entered
srchPerson object to execute the search query. Again, I am just showing a
couple of my search fields:

if ( q != null ) {
    q.setParameter("lastName", srchPerson.getLastName());
    q.setParameter("member", srchPerson.getMember());

    List<Person> results = q.getResultList();
    if ( results != null ) {
        for (Iterator i = results.iterator(); i.hasNext();) {
            Person p = (Person) i.next();
            // do stuff with the Person object here...
        }
    }
}

The query works great and I get my expected results. Although I didn't show
all of my search fields here I have a total of nine fields and depending if
I include them in the srchPerson object the method correctly builds the
expression for the query.

The problems I have not solved in this approach are -
1. I haven't figured out how to add an ORDER BY expression in my expression
building routine.
2. I haven't figured out how to add in relationship table columns in the
query using the ExpressionBuilder.
3. Is it good practice to use the Toplink ExpressionBuilder and Expression
classes.
4. Is the Toplink ExpressionBuilder proprietary and not portable to other
JPA managers.
5. Is it good practice to construct the Query object as I did. I found some
examples of using a ReadAllQuery class but it looks like it is a lot
different and might also be also be a Toplink only solution.
-- 
View this message in context: http://www.nabble.com/Is-using-Toplink-ExpressionBuilder-a-good-choice-tp18645581p18645581.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.