Description | On many occasions when we perform a query with DAL, we do not do it quite well from a performance point of view. Here are some rules we should always follow:
• When we just want to know the number of records returned by the query, we usually do: query.list().size(). However it is better to do query.count(). If in addition to the number of records, we want to go through them, then it is better to store in a variable the result: list = query.list() and then look at its size: list.size().
• If, in addition to scrolling through the list, we want to know if we have at least one record, what we should do is: list.isEmpty(), instead of list.size ()> 0.
• It is important not to ever run the query.list() more than once, since that throws the same query twice. To do this, we execute the query only once and store it in a variable.
• When we only want to access the first record returned by the query, then we must use query.setMaxResults(1) in case the query returns more than one record, and we will return it using object = query.uniqueResult() instead of doing query.list().get(0).
• If you want to see if the query returns a record or not, just do the previous point and see if object ! = null.
These issues are found in many places along OB. For each of these cases code should be adapted to behave as explained above. |
Attached Files | 0.diff [^] (14,677 bytes) 2017-09-19 18:52 [Show Content] [Hide Content]diff --git a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java
@@ -259,7 +259,7 @@
whereClause.append(" and (oinfo is null or oinfo.active = true)");
// remove selected payments
- if (selectedScheduledPaymentDetails != null && selectedScheduledPaymentDetails.size() > 0) {
+ if (selectedScheduledPaymentDetails != null && !selectedScheduledPaymentDetails.isEmpty()) {
String strSelectedPaymentDetails = Utility.getInStrList(selectedScheduledPaymentDetails);
whereClause.append(" and psd not in (");
whereClause.append(strSelectedPaymentDetails);
@@ -270,9 +270,10 @@
final OBCriteria<FIN_PaymentPropDetail> obc = OBDal.getInstance().createCriteria(
FIN_PaymentPropDetail.class);
obc.add(Restrictions.isNotNull(FIN_PaymentPropDetail.PROPERTY_FINPAYMENTSCHEDULEDETAIL));
- if (obc.list() != null && obc.list().size() > 0) {
+ List<FIN_PaymentPropDetail> paymentPropDetailList = obc.list();
+ if (!paymentPropDetailList.isEmpty()) {
List<FIN_PaymentScheduleDetail> aux = new ArrayList<FIN_PaymentScheduleDetail>();
- for (FIN_PaymentPropDetail ppd : obc.list()) {
+ for (FIN_PaymentPropDetail ppd : paymentPropDetailList) {
aux.add(ppd.getFINPaymentScheduledetail());
}
whereClause.append(" and psd.id not in (" + Utility.getInStrList(aux) + ")");
@@ -1153,7 +1154,7 @@
FIN_FinaccTransaction.class, whereClause.toString());
obqFATrx.setFirstResult(offset);
obqFATrx.setMaxResult(maxrowspergridpage);
- return obqFATrx.list().size();
+ return obqFATrx.count();
} finally {
OBContext.restorePreviousMode();
@@ -1545,28 +1546,30 @@
}
obc.addOrder(org.hibernate.criterion.Order.asc("pm." + FIN_PaymentMethod.PROPERTY_NAME));
obc.setFilterOnReadableOrganization(false);
- List<FinAccPaymentMethod> defaults = obc.list();
- if (defaults.size() > 0) {
- return obc.list().get(0).getPaymentMethod().getId();
+ obc.setMaxResults(1);
+ FinAccPaymentMethod paymentMethod = (FinAccPaymentMethod) obc.uniqueResult();
+ if (paymentMethod != null) {
+ return paymentMethod.getPaymentMethod().getId();
} else {
return "";
}
}
private void addPaymentMethodList(OBCriteria<FIN_PaymentMethod> obc, List<String> paymentMethods) {
- List<String> paymentMethodsToRemove;
Criterion compoundExp = null;
- while (paymentMethods.size() > 999) {
- paymentMethodsToRemove = new ArrayList<String>(paymentMethods.subList(0, 999));
+ int paymentMethodsSize = paymentMethods.size();
+ while (paymentMethodsSize > 999) {
+ List<String> paymentMethodsToRemove = paymentMethods.subList(0, 999);
if (compoundExp == null) {
- compoundExp = Restrictions.in("id", paymentMethods.subList(0, 999));
+ compoundExp = Restrictions.in("id", paymentMethodsToRemove);
} else {
compoundExp = Restrictions.or(compoundExp,
Restrictions.in("id", paymentMethods.subList(0, 999)));
}
paymentMethods.removeAll(paymentMethodsToRemove);
+ paymentMethodsSize = paymentMethodsSize - 999;
}
- if (paymentMethods.size() > 0) {
+ if (paymentMethodsSize > 0) {
if (compoundExp == null) {
compoundExp = Restrictions.in("id", paymentMethods);
} else {
@@ -1676,14 +1679,14 @@
}
public Criterion getCriterion() {
- if (finAccs.size() > 0) {
+ if (!finAccs.isEmpty()) {
refresh();
}
return compoundexp;
}
private void refresh() {
- // finAccs.size() must be > 0
+ // finAccs size must be > 0
if (compoundexp == null) {
compoundexp = Restrictions.in("id", finAccs);
} else {
@@ -1879,9 +1882,10 @@
OBCriteria<APRMPendingPaymentFromInvoice> ppfiCriteria = OBDal.getInstance().createCriteria(
APRMPendingPaymentFromInvoice.class);
ppfiCriteria.add(Restrictions.eq(APRMPendingPaymentFromInvoice.PROPERTY_PAYMENT, payment));
- List<APRMPendingPaymentFromInvoice> pendingPayments = ppfiCriteria.list();
- if (pendingPayments != null && pendingPayments.size() > 0) {
- APRMPendingPaymentFromInvoice pendingPayment = pendingPayments.get(0);
+ ppfiCriteria.setMaxResults(1);
+ APRMPendingPaymentFromInvoice pendingPayment = (APRMPendingPaymentFromInvoice) ppfiCriteria
+ .uniqueResult();
+ if (pendingPayment != null) {
pendingPayment.setProcessNow(executing);
OBDal.getInstance().flush();
OBDal.getInstance().save(pendingPayment);
@@ -1892,12 +1896,14 @@
OBCriteria<APRMPendingPaymentFromInvoice> ppfiCriteria = OBDal.getInstance().createCriteria(
APRMPendingPaymentFromInvoice.class);
ppfiCriteria.add(Restrictions.eq(APRMPendingPaymentFromInvoice.PROPERTY_PAYMENT, payment));
- List<APRMPendingPaymentFromInvoice> pendingPayments = ppfiCriteria.list();
- if (pendingPayments != null && pendingPayments.size() > 0) {
- return pendingPayments.get(0).isProcessNow();
- } else
+ ppfiCriteria.setMaxResults(1);
+ APRMPendingPaymentFromInvoice pendingPayment = (APRMPendingPaymentFromInvoice) ppfiCriteria
+ .uniqueResult();
+ if (pendingPayment != null) {
+ return pendingPayment.isProcessNow();
+ } else {
return false;
-
+ }
}
public void removeFromExecutionPending(FIN_Payment payment) {
@@ -2047,15 +2053,9 @@
obc.add(Restrictions.in("organization.id", OBContext.getOBContext()
.getOrganizationStructureProvider().getNaturalTree(strOrgId)));
obc.setFilterOnReadableOrganization(false);
-
obc.add(Restrictions.eq(FIN_FinancialAccount.PROPERTY_DEFAULT, true));
-
- final List<FIN_FinancialAccount> defaultAccounts = obc.list();
- if (defaultAccounts.size() > 0) {
- return defaultAccounts.get(0);
- } else {
- return null;
- }
+ obc.setMaxResults(1);
+ return (FIN_FinancialAccount) obc.uniqueResult();
}
public boolean existsAPRMReadyPreference() {
@@ -2111,13 +2111,7 @@
.getOrganizationStructureProvider().getNaturalTree(organization.getId())));
obcFinPayment.addOrderBy(FIN_Payment.PROPERTY_PAYMENTDATE, true);
obcFinPayment.setMaxResults(1);
-
- final List<FIN_Payment> finPayments = obcFinPayment.list();
- if (finPayments.size() > 0) {
- return finPayments.get(0);
- } else {
- return null;
- }
+ return (FIN_Payment) obcFinPayment.uniqueResult();
} finally {
OBContext.restorePreviousMode();
}
diff --git a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
--- a/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
+++ b/modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java
@@ -83,9 +83,7 @@
FIN_FinaccTransaction.class, whereClause.toString());
obQuery.setParameters(parameters);
OBObjectFieldProvider[] objectFieldProvider = null;
- if (obQuery != null && obQuery.list().size() > 0) {
- objectFieldProvider = OBObjectFieldProvider.createOBObjectFieldProvider(obQuery.list());
- }
+ objectFieldProvider = OBObjectFieldProvider.createOBObjectFieldProvider(obQuery.list());
return objectFieldProvider;
} finally {
OBContext.restorePreviousMode();
@@ -216,10 +214,7 @@
obc.addOrderBy(FIN_Reconciliation.PROPERTY_ENDINGDATE, false);
obc.addOrderBy(FIN_Reconciliation.PROPERTY_CREATIONDATE, false);
obc.setMaxResults(1);
- final List<FIN_Reconciliation> rec = obc.list();
- if (rec.size() == 0)
- return null;
- return rec.get(0);
+ return (FIN_Reconciliation) obc.uniqueResult();
} finally {
OBContext.restorePreviousMode();
}
@@ -240,7 +235,7 @@
.append(" is null ");
final OBQuery<FIN_FinaccTransaction> obqFATrx = OBDal.getInstance().createQuery(
FIN_FinaccTransaction.class, whereClause.toString());
- return obqFATrx.list().size();
+ return obqFATrx.count();
} finally {
OBContext.restorePreviousMode();
@@ -262,14 +257,11 @@
}
public static Period getPeriod(Date date) {
- Period period = null;
OBCriteria<Period> obcPe = OBDal.getInstance().createCriteria(Period.class);
obcPe.add(Restrictions.le(Period.PROPERTY_ENDINGDATE, date));
obcPe.add(Restrictions.ge(Period.PROPERTY_STARTINGDATE, date));
- if (obcPe.list() != null && obcPe.list().size() > 0) {
- period = obcPe.list().get(0);
- }
- return period;
+ obcPe.setMaxResults(1);
+ return (Period) obcPe.uniqueResult();
}
public static List<FIN_FinaccTransaction> getTransactionsToReconciled(
diff --git a/src-test/src/org/openbravo/test/costing/TestCosting.java b/src-test/src/org/openbravo/test/costing/TestCosting.java
--- a/src-test/src/org/openbravo/test/costing/TestCosting.java
+++ b/src-test/src/org/openbravo/test/costing/TestCosting.java
@@ -10269,7 +10269,7 @@
try {
final OBCriteria<Product> criteria = OBDal.getInstance().createCriteria(Product.class);
criteria.add(Restrictions.like(Product.PROPERTY_NAME, name + "-%"));
- return criteria.list().size();
+ return criteria.count();
} catch (Exception e) {
throw new OBException(e);
}
diff --git a/src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java b/src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java
--- a/src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java
+++ b/src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java
@@ -593,7 +593,7 @@
try {
final OBCriteria<Locator> criteria = OBDal.getInstance().createCriteria(Locator.class);
criteria.add(Restrictions.like(Locator.PROPERTY_SEARCHKEY, searchKey + "-%"));
- return criteria.list().size();
+ return criteria.count();
} catch (Exception e) {
throw new OBException(e);
}
@@ -647,7 +647,7 @@
try {
final OBCriteria<Product> criteria = OBDal.getInstance().createCriteria(Product.class);
criteria.add(Restrictions.like(Product.PROPERTY_NAME, name + "-%"));
- return criteria.list().size();
+ return criteria.count();
} catch (Exception e) {
throw new OBException(e);
}
@@ -723,7 +723,7 @@
final OBCriteria<ShipmentInOut> criteria = OBDal.getInstance().createCriteria(
ShipmentInOut.class);
criteria.add(Restrictions.like(ShipmentInOut.PROPERTY_DOCUMENTNO, docNo + "-%"));
- return criteria.list().size();
+ return criteria.count();
} catch (Exception e) {
throw new OBException(e);
}
diff --git a/src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java b/src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java
--- a/src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java
+++ b/src/org/openbravo/erpCommon/ad_callouts/SE_Invoice_BPartner.java
@@ -253,7 +253,7 @@
obc.add(Restrictions.in(FinAccPaymentMethod.PROPERTY_ORGANIZATION + ".id", OBContext
.getOBContext().getOrganizationStructureProvider().getNaturalTree(strOrgId)));
- // filter is on unique constraint so list().size() <=1 always
+ // filter is on unique constraint so list size <=1 always
if (obc.uniqueResult() == null) {
message = Utility.messageBD(this, "PaymentmethodNotbelongsFinAccount",
vars.getLanguage());
diff --git a/src/org/openbravo/erpCommon/ad_callouts/SE_Locator_Activate.java b/src/org/openbravo/erpCommon/ad_callouts/SE_Locator_Activate.java
--- a/src/org/openbravo/erpCommon/ad_callouts/SE_Locator_Activate.java
+++ b/src/org/openbravo/erpCommon/ad_callouts/SE_Locator_Activate.java
@@ -100,7 +100,8 @@
final OBQuery<StorageDetail> query = OBDal.getInstance().createQuery(StorageDetail.class,
hsqlScript.toString());
query.setParameters(parameters);
- return query.list().size() > 0;
+ query.setMaxResult(1);
+ return query.uniqueResult() != null;
}
/**
@@ -124,7 +125,8 @@
final OBQuery<StoragePending> query = OBDal.getInstance().createQuery(StoragePending.class,
hsqlScript.toString());
query.setParameters(parameters);
- return query.list().size() > 0;
+ query.setMaxResult(1);
+ return query.uniqueResult() != null;
}
/**
diff --git a/src/org/openbravo/erpCommon/utility/InventoryStatusUtils.java b/src/org/openbravo/erpCommon/utility/InventoryStatusUtils.java
--- a/src/org/openbravo/erpCommon/utility/InventoryStatusUtils.java
+++ b/src/org/openbravo/erpCommon/utility/InventoryStatusUtils.java
@@ -124,7 +124,7 @@
obc.add(Restrictions.eq(Locator.PROPERTY_ISVIRTUAL, true));
obc.add(Restrictions.eq(Locator.PROPERTY_PARENTLOCATOR, storageBin));
obc.setFilterOnActive(active);
- return obc.list().size();
+ return obc.count();
}
/**
diff --git a/src/org/openbravo/erpCommon/utility/Utility.java b/src/org/openbravo/erpCommon/utility/Utility.java
--- a/src/org/openbravo/erpCommon/utility/Utility.java
+++ b/src/org/openbravo/erpCommon/utility/Utility.java
@@ -1844,9 +1844,11 @@
+ " and rlt.language.language = '" + lang + "'" + " and r.name = '" + ListName + "'"
+ " and rl.searchKey = '" + value + "'";
Query q = OBDal.getInstance().getSession().createQuery(hql);
+ q.setMaxResults(1);
- if (q.list().size() > 0) {
- return (String) q.list().get(0);
+ String name = (String) q.uniqueResult();
+ if (name != null) {
+ return name;
}
// No translated value obtained, get the standard one
@@ -1854,9 +1856,11 @@
+ " where rl.reference = r" + " and r.name = '" + ListName + "'"
+ " and rl.searchKey = '" + value + "'";
q = OBDal.getInstance().getSession().createQuery(hql);
+ q.setMaxResults(1);
- if (q.list().size() > 0) {
- return (String) q.list().get(0);
+ name = (String) q.uniqueResult();
+ if (name != null) {
+ return name;
} else {
// Nothing found, return the value
return value;
0.txt [^] (962 bytes) 2017-09-19 18:53 [Show Content] [Hide Content]./src/org/openbravo/erpCommon/utility/InventoryStatusUtils.java: return obc.list().size();
./src-test/src/org/openbravo/test/costing/TestCosting.java: return criteria.list().size();
./src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java: return criteria.list().size();
./src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java: return criteria.list().size();
./src-test/src/org/openbravo/test/inventoryStatus/InventoryStatusTest.java: return criteria.list().size();
./modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/TransactionsDao.java: return obqFATrx.list().size();
./modules/org.openbravo.advpaymentmngt/src/org/openbravo/advpaymentmngt/dao/AdvPaymentMngtDao.java: return obqFATrx.list().size();
./modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java: return attachmentFiles.list().size();
|