Menu

#680 Query. first/firstOnly usage best practice

open
5
2009-03-20
2009-03-19
No

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

Discussion

  • Cristina Ghita

    Cristina Ghita - 2009-03-19
    • assigned_to: nobody --> teo_sarca
     
  • Teo Sarca

    Teo Sarca - 2009-03-19
    • summary: Query class --> Query. first/firstOnly usage best practice
     
  • Carlos Ruiz

    Carlos Ruiz - 2009-03-19

    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

     
  • Teo Sarca

    Teo Sarca - 2009-03-19

    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

     
  • Carlos Ruiz

    Carlos Ruiz - 2009-03-20

    > 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

     
  • Carlos Ruiz

    Carlos Ruiz - 2009-03-20
    • status: open --> pending
     
  • Carlos Ruiz

    Carlos Ruiz - 2009-03-20

    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

     
  • Carlos Ruiz

    Carlos Ruiz - 2009-03-20
    • status: pending --> open
     
  • Teo Sarca

    Teo Sarca - 2009-03-20

    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

     

Log in to post a comment.

MongoDB Logo MongoDB