Example: Dynamic query from local client

Establish a transaction context prior to calling executeQuery so that a call to begin( ) and commit( ) is needed. The QueryLocalIterator becomes invalid at end of a transaction. Therefore, you must use the iterator in the same transaction scope as the executeQuery call.

import com.ibm.websphere.ejbquery.QueryLocalHome;
  import com.ibm.websphere.ejbquery.QueryLocal;
  import com.ibm.websphere.ejbquery.QueryLocalIterator;
  import com.ibm.websphere.ejbquery.IQueryTuple;
  import com.ibm.websphere.ejbquery.QueryException;

String query = 
"select e.name, object(e) from EmpBean e where e.salary < 50000 ";

InitialContext ic =  new InitialContext(); 
   QueryLocalHome  qh =  ( LocalQueryHome) ic.lookup( "java:comp/env/ejb/query" );
QueryLocal qb = qh.create();
userTransaction.begin();
QueryLocalIterator it = qb.executeQuery(query, null, null);
while (it.hasNext() ) { 
IQueryTuple tuple = (IQueryTuple) it.next();
System.out.print( it.getFieldName(1) );
String s = (String) tuple.getObject(1);
System.out.println( s);

System.out.println( it.getFieldName(2) );
EmpLocal e = ( EmpLocal ) tuple.getObject(2);
System.out.println( e.getPrimaryKey().toString());
}
userTransaction.commit();

Related tasks
Using the dynamic query service



Searchable topic ID:   xque_localclient
Last updated: Jun 21, 2007 8:07:48 PM CDT    WebSphere Business Integration Server Foundation, Version 5.0.2
http://publib.boulder.ibm.com/infocenter/wasinfo/index.jsp?topic=/com.ibm.wasee.doc/info/ee/query/xmp/xque_localclient.html

Library | Support | Terms of Use | Feedback