Re: EJBQL with aggregrate function render to Java List

From: Steve Wells (websystem..mail.com)
Date: Tue Jan 12 2010 - 19:15:00 EST

  • Next message: Mark Fraser: "Re: Expression help"

    Thanks Andrey.

    It would be a nice to have, for now I have gone with:

    EJBQLQuery query = new EJBQLQuery(
                    "select c.name, count(q) from Category c left join
    c.questions q group by c.name");
            List list = performQuery(query);
            List<CategorySummary> categorySummaries = new
    ArrayList<CategorySummary>(list.size());
            for (Object o : list) {
                Object[] result = (Object[]) o;
                CategorySummary summary = new CategorySummary((String)
    result[0], (Number) result[1]);
                categorySummaries.add(summary);
                getLog().debug("Adding category summary: " + summary);
            }

    2010/1/12 Andrey Razumovsky <razumovsky.andre..mail.com>

    > Nope, unfortunately (correct me if i'm wrong) Cayenne doesn't support
    > constructors in EJBQL (yet)
    >
    > 2010/1/12 Steve Wells <websystem..mail.com>
    >
    > > Sorry forgot to add using Cay3-RC1 (congrats on the release!)
    > >
    > > 2010/1/12 Steve Wells <websystem..mail.com>
    > >
    > > > Hi All,
    > > >
    > > > I have an EJBQL query selecting a grouped value from a master table
    > > > (Category.name) and the count of detail rows for each grouping (ie
    > count
    > > of
    > > > the Category.questions), so I get results:
    > > > cat1, 5
    > > > cat2, 3
    > > > cat3, 10
    > > > etc...
    > > >
    > > > Ideally what I need to do is get the results into a java List<?>, so I
    > > was
    > > > hoping (by waving magic Cayenne wand!) that this would work:
    > > >
    > > > "select new CategorySummary(c.name, count(q)) from Category c left
    > join
    > > > c.questions q group by c.name" returning a List<CategorySummary>...but
    > I
    > > > can't get it working. Is there a prescribed approach for this?
    > > >
    > > > At the moment I get this:
    > > >
    > > > java.lang.NullPointerException
    > > > at
    > >
    > org.apache.cayenne.ejbql.parser.Compiler.compileEntityResult(Compiler.java:219)
    > > > at
    > > org.apache.cayenne.ejbql.parser.Compiler.compile(Compiler.java:200)
    > > > at
    > >
    > org.apache.cayenne.ejbql.parser.EJBQL$EJBQLDefaultParser.compile(EJBQL.java:30)
    > > > at
    > > org.apache.cayenne.query.EJBQLQuery.getExpression(EJBQLQuery.java:122)
    > > > at
    > >
    > org.apache.cayenne.query.EJBQLQueryMetadata.resolve(EJBQLQueryMetadata.java:38)
    > > > at
    > > org.apache.cayenne.query.EJBQLQuery.getMetaData(EJBQLQuery.java:66)
    > > > at
    > >
    > org.apache.cayenne.util.ObjectContextQueryAction.<init>(ObjectContextQueryAction.java:79)
    > > > at
    > >
    > org.apache.cayenne.access.DataContextQueryAction.<init>(DataContextQueryAction.java:48)
    > > > at
    > > org.apache.cayenne.access.DataContext.onQuery(DataContext.java:1278)
    > > > at
    > > org.apache.cayenne.access.DataContext.performQuery(DataContext.java:1267)
    > > > at
    > >
    > org.apache.click.extras.cayenne.CayenneTemplate.performQuery(CayenneTemplate.java:276)
    > > >
    > > >
    > > >
    > > > CategorySummary.java:
    > > >
    > > > CategorySummary {
    > > > private String name;
    > > > private Number count;
    > > >
    > > > plus getters and setters
    > > >
    > > > The EJB spec says you can do something like:
    > > >
    > > > SELECT NEW com.acme.example.CustomerDetails(c.id, c.status, o.count)
    > > > FROM Customer c JOIN c.orders o
    > > > WHERE o.count > 100
    > > >
    > > > Does Cayenne support this?
    > > >
    > > > Thanks for any help,
    > > >
    > > > Steve
    > > >
    > >
    >
    >
    >
    > --
    > Andrey
    >



    This archive was generated by hypermail 2.0.0 : Tue Jan 12 2010 - 19:15:38 EST