Using the Query Tool

From Opentaps Wiki
Revision as of 17:49, 4 June 2008 by Sichen (talk | contribs) (How It Works)
Jump to navigationJump to search

A common problem with the ofbiz entity engine and delegator is that it could be difficult to construct complex queries. To solve this problem, we created an opentaps query tool, which works with the ofbiz delegator but allows you to write your queries in familiar SQL. The query tool could then return either lists or EntityListIterator of GenericEntity or simply lists of maps.

How It Works

To use the query tool, create a QueryFactory from the delegator:

  String entityGroupName = "org.ofbiz";
  QueryFactory qf = new QueryFactory(delegator, entityGroupName);

Then, use your QueryFactory to create either an EntityQuery or a Query. The syntax should be similar to Hibernate querying. The EntityQuery can be used to cast the result of your query as lists or an EntityListIterator of GenericEntity:

  EntityQuery query1 = qf.createEntityQuery("StatusItem", "SELECT * FROM STATUS_ITEM WHERE STATUS_TYPE_ID = 'INVOICE_STATUS'");
  List list1 = query1.list();
  EntityListIterator eli = query1.eli();
  Iterator it2 =;

The Query object can be used to pass prepared statements to your database through the delegator's connection and return either a list, an iterator, or result set:

  Query query2 = qf.createQuery("StatusItem", "SELECT * FROM STATUS_ITEM WHERE STATUS_TYPE_ID = ?");
  query2.setString(1, "INVOICE_STATUS");
  List list2 = query2.list();
  Iterator it2 = query2.iterator();
  ResultSet rs = query2.resultSet();
  ResultSetMetaData rsm = query2.resultSetMetaData();

Every object in the List returned from a Query will be a Map.