4

Let's say we have to fetch only 5 records from a table but my where clause is matching 25k records in the database. So is there a way in ofbiz framework to just select 5 records rather than getting a list from the database and then taking just 5 from the list?

If the limit is not possible (since ofbiz API is database agnostic) what are my other alternatives?

anubhava
  • 761,203
  • 64
  • 569
  • 643

1 Answers1

5

I would suggest you take a look into this entity engine cookbook

Essentially for getting limited set of rows from database you would do:

// first get a list iterator
productsELI = delegator.findListIteratorByCondition("Product", 
  new EntityExpr("productId", EntityOperator.NOT_EQUAL, null), 
                  UtilMisc.toList("productId"), null);

// then get a partial list by count TO RETURN first 5 records
productsELI.getPartialList(0, 5);

// and finally just close the iterator
productsELI.close();

Also if you prefer to send direct SQL to your database then just do like this:

// gets the helper (localmysql, localpostgres, etc.) for your entity group org.ofbiz
String helperName = delegator.getGroupHelperName("org.ofbiz");
SQLProcessor sqlproc = new SQLProcessor(helperName);
sqlproc.prepareStatement("SELECT * FROM PARTY LMIT 0, 5");
ResultSet rs1 = sqlproc.executeQuery();

// and then get your data from ResultSet like regular JDBC
Siddharth Mehra
  • 1,691
  • 1
  • 9
  • 32
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • How can you take String type parameter in SQLProcessor constructor ? There are no String Type parameter in SQLProcessor constructor. – Newaz Sharif Amit Aug 07 '16 at 03:34
  • [There is an API to set string value](https://ci.apache.org/projects/ofbiz/site/javadocs/org/ofbiz/entity/jdbc/SQLProcessor.html#setValue-java.lang.String-) – anubhava Aug 07 '16 at 05:00