2 Replies Latest reply on Dec 1, 2003 6:36 AM by draftdog

    ql and COUNT() - return type ?

    nefilim

      Hi,

      I don't know if this is the best way to do it but I figured it's less expensive to have a select method in the home interface of a cmp that counts the amount of rows for a specific condition than to find them all and count the collection.

      The problem is though, according to Denniger & Peter's EJB 2.1 select methods can only return type of remote or local interface or of a persistent attribute. So I come up with this query:

      SELECT COUNT(p.id) FROM User AS p WHERE p.deptId=?1

      However, deploying on JBoss 3.2.2 I get the following error:

      (org.jboss.ejb.plugins.cmp.ejbql.ParseException: Encountered "COUNT" at line 1, column 8.
      Was expecting one of:
      "DISTINCT" ...
      "OBJECT" ...
      <NUMERIC_VALUED_PATH> ...
      <STRING_VALUED_PATH> ...
      <DATETIME_VALUED_PATH> ...
      <BOOLEAN_VALUED_PATH> ...
      <ENTITY_VALUED_PATH> ...
      <VALUE_CLASS_VALUED_PATH> ...
      )

      Is COUNT() not supported yet or am I not using it correctly? Btw, what does one do if the primary key is not numeric, the return type would not match the persistent attribute type? Is there a better way of achieving this?

      Many thanks,
      Peter

        • 1. Re: ql and COUNT() - return type ?
          draftdog

          hi Peter,

          make sure you implement this method as an select method and not as a finder method

          assuming you're using xdoclet you will need to add a class-level tag @ejb.select and specify the query there, you will then also need to write a method for this query

          there are many examples to be found on the internet that explain how to achieve this, but let me point you to a working example of mine, it is a bean generated by AndroMDA using a custom template, it automatically generates some default finders and selects

          http://cvs.sourceforge.net/viewcvs.py/jeteam/ejb/src/java/org/jeteam/business/note/NoteBean.java?view=markup

          my primary keys are Strings while the COUNT returns a java.lang.Long


          hth
          Wouter.

          • 2. Re: ql and COUNT() - return type ?
            draftdog

            correction, it is not @ejb.select but @jboss.query you will need to add at the class level

            this will make ejbdoclet render this as JBoss-QL (also notice the 'dynamic' attribute (for dynamic-ql)

            anyway, taking a look at the tags + methods should give you an idea