Difference between revisions of "Using the Query Tool"
Line 3: | Line 3: | ||
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. | 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. | ||
− | Another benefit of the query tool is that it allows you to use JDBC prepared statements, which is more efficient because the query can be reused for different parameters, rather than a different query being passed to your database each time, which is the case with the delegator. | + | Another benefit of the query tool is that it allows you to use JDBC prepared statements, which is more efficient because the query can be reused for different parameters, rather than a different query being passed to your database each time, which is the case with the delegator. (See [http://www.onjava.com/pub/a/onjava/excerpt/oraclejdbc_19/index.html this article about JDBC performance tuning].) |
== How It Works == | == How It Works == |
Revision as of 00:59, 5 June 2008
This feature is under development and is not available for general use yet.
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.
Another benefit of the query tool is that it allows you to use JDBC prepared statements, which is more efficient because the query can be reused for different parameters, rather than a different query being passed to your database each time, which is the case with the delegator. (See this article about JDBC performance tuning.)
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 = query1.it();
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.