Search code examples
javacounttoplink

How can I get the result of a Count query in TopLink?


I have this:

ReportQuery query = new ReportQuery(OpenedFilesReport.class,
  generateExpressionOpenedFilesReport());
query.addCount();

Object result = getTopLinkTemplate().executeQuery(query, true);

As I can see, the result is a Vector and has one result of the type ReportQueryResult. Is there a smarter way of getting the result then

((ReportQueryResult)((Vector)result).get(0)).getResults()//.. do additional class casts/getter calls

Solution

  • query.setShouldReturnSingleValue(true);

    Number result = (Number) getTopLinkTemplate().executeQuery(query, true);

    Here is the link into the EclipseLink JavaDocs:

    http://www.eclipse.org/eclipselink/api/2.2/org/eclipse/persistence/queries/ReportQuery.html#setShouldReturnSingleValue%28boolean%29

    Doug