Hi,
I think that when we use method first, always should be used with an order by clause. In this way, can protect us from mistakes. Because when the query returns more then one, if you have no order, the first is random.
The best practice is that when you use method first, to set an order by clause.
But, if you know that from your where clause logic, the record is unique, then use firstOnly() method.
Regards,
Cristina Ghita - www.arhipac.ro
Hi Cristina,
first is faster than firstOnly - I think it's useful even when you're sure the query is returning one value.
So, I'm against the idea to put a restriction in Query to compel the usage of orderBy when using firstOnly.
I agree with you that if the query is multi-record the developer must take care of adding orderBy clause, but not as restriction on Query.
Regards,
Carlos Ruiz
Hi Carlos,
> first is faster than firstOnly - I think it's useful even when you're sure
> the query is returning one value.
The performance difference is not significant.
> I agree with you that if the query is multi-record the developer must take
> care of adding orderBy clause, but not as restriction on Query.
Agree. We do not want to enforce to Adempiere trunk. As a matter of fact, in our private branch we modified the Query.first method as follows:
@SuppressWarnings("unchecked")
public <T extends PO> T first() throws DBException
{
// arhipac: teo_sarca: begin
if (org.compiere.util.ARHIPAC.isDebugging() && Util.isEmpty(this.orderBy, true))
{
throw new DBException("Developer Error: You are using first() method but no ORDER BY clause was specified");
}
// arhipac: teo_sarca end
..........
So in our private branch, when ADempiere is run from Eclipse (so developer runs adempiere), it throws an exception for this situation because is not best practice.
Anyway i did not propose to change the Query method, we just proposed a best practice that can prevent you from hard to detect errors.
Best regards,
Teo Sarca - www.arhipac.ro
> The performance difference is not significant.
It depends on the workload - one million of first will be different than one million of firstOnly.
Regards,
Carlos Ruiz
Hmm - I didn't see revision 8770 is related to this - so I guess this is a long term task.
Please cross-reference commits in trackers.
Regards,
Carlos Ruiz
Revision: 8770
http://adempiere.svn.sourceforge.net/adempiere/?rev=8770&view=rev
Author: teo_sarca
Date: 2009-03-19 21:46:29 +0000 (Thu, 19 Mar 2009)
Log Message:
-----------
FR [ 2694043 ] Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2694043&group_id=176962
Modified Paths:
--------------
trunk/zkwebui/WEB-INF/src/org/adempiere/webui/util/UserPreference.java
Revision: 8783
http://adempiere.svn.sourceforge.net/adempiere/?rev=8783&view=rev
Author: teo_sarca
Date: 2009-03-20 09:40:19 +0000 (Fri, 20 Mar 2009)
Log Message:
-----------
FR [ 2694043 ] Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2694043&group_id=176962
Modified Paths:
--------------
trunk/base/src/org/compiere/model/MAccountLookup.java
trunk/base/src/org/compiere/model/MAcctSchemaGL.java
trunk/base/src/org/compiere/model/MBPartner.java
trunk/base/src/org/compiere/model/MLanguage.java
trunk/base/src/org/compiere/model/MOrgInfo.java
trunk/base/src/org/compiere/model/MProductCategoryAcct.java
Revision: 8997
http://adempiere.svn.sourceforge.net/adempiere/?rev=8997&view=rev
Author: teo_sarca
Date: 2009-04-14 07:07:40 +0000 (Tue, 14 Apr 2009)
Log Message:
-----------
Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2694043&group_id=176962
Modified Paths:
--------------
trunk/base/src/org/compiere/model/MAccount.java
Revision: 9007
http://adempiere.svn.sourceforge.net/adempiere/?rev=9007&view=rev
Author: teo_sarca
Date: 2009-04-14 08:23:52 +0000 (Tue, 14 Apr 2009)
Log Message:
-----------
FR [ 2694043 ] Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/index.php?func=detail&aid=2694043&group_id=176962&atid=879335
Modified Paths:
--------------
trunk/base/src/org/compiere/model/MRefList.java
Revision: 9093
http://adempiere.svn.sourceforge.net/adempiere/?rev=9093&view=rev
Author: teo_sarca
Date: 2009-04-24 13:31:07 +0000 (Fri, 24 Apr 2009)
Log Message:
-----------
Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2694043&group_id=176962
Modified Paths:
--------------
trunk/base/src/org/compiere/model/MBPartner.java
Fixed MProductCosting in http://adempiere.svn.sourceforge.net/viewvc/adempiere?view=rev&revision=9571
Revision: 9771
http://adempiere.svn.sourceforge.net/adempiere/?rev=9771&view=rev
Author: teo_sarca
Date: 2009-07-29 09:22:59 +0000 (Wed, 29 Jul 2009)
Log Message:
-----------
[ 2694043 ] Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2694043&group_id=176962
Modified Paths:
--------------
trunk/base/src/org/compiere/model/MAcctSchemaDefault.java
Revision: 10412
http://adempiere.svn.sourceforge.net/adempiere/?rev=10412&view=rev
Author: teo_sarca
Date: 2009-09-28 09:56:19 +0000 (Mon, 28 Sep 2009)
Log Message:
-----------
[ 2694043 ] Query. first/firstOnly usage best practice
https://sourceforge.net/tracker/?func=detail&atid=879335&aid=2694043&group_id=176962
Modified Paths:
--------------
trunk/base/src/org/compiere/model/MCurrency.java