[jboss-user] [JBoss Seam] - count(*) and group by

artur.chyzy do-not-reply at jboss.com
Thu Jul 19 07:14:53 EDT 2007


Hello. 
I'm trying to show a list of objects. 
I'm using EntityQuery. 

I also show a message when list is empty so i put 

  | rendered="#{list.resultCount == 0}"
  | 
on outputText but the excaption is thrown. 
The problem is that my query has  group by i JPQL

In query class method getCountEjbql

  | protected String getCountEjbql()
  |    {
  |       String ejbql = getRenderedEjbql();
  |       
  |       Matcher fromMatcher = FROM_PATTERN.matcher(ejbql);
  |       if ( !fromMatcher.find() )
  |       {
  |          throw new IllegalArgumentException("no from clause found in query");
  |       }
  |       int fromLoc = fromMatcher.start(2);
  |       
  |       Matcher orderMatcher = ORDER_PATTERN.matcher(ejbql);
  |       int orderLoc = orderMatcher.find() ? orderMatcher.start(1) : ejbql.length();
  | 
  |       return "select count(*) " + ejbql.substring(fromLoc, orderLoc);
  |    }
  | 

is only checking for order by and from. 
The count(*) is insterted before all... but it will not work with group by queries because then the list of objects will be in result but seam gets getSingleResult in getResultCount method and the exception will be thrown..


  | public Long getResultCount()
  |    {
  |       if ( resultCount==null || isAnyParameterDirty() )
  |       {
  |          javax.persistence.Query query = createCountQuery();
  |          resultCount = query==null ? 
  |                null : (Long) query.getSingleResult();
  |       }
  |       return resultCount;
  |    }
  | 

For now i made a workaround

  | rendered="#{not empty lista.resultCount }"
  | 

but i think that everybody can see how this is looking like... terrible

i think that when there is group by in query then it should be converted like this for count(*)

  | select count(*) from (previous_query ) as some_name
  | 

Is this a bug or i'm doing sth wrong ??

Can someone put some light into my problem ??

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4065733#4065733

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4065733



More information about the jboss-user mailing list