Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to count the number of rows of a JPA 2 CriteriaQuery in a generic JPA DAO?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new in JPA and want to implement a generic JPA DAO and need to find the number of rows of a query result set to implement pagination. After searching the web, I can't find a practical way to do that. Here is the code suggested in many articles:

However, that code doesn't work when using `join`. Is there any way to count the rows of a query result set using the JPA Criteria API?

here is the code that create CriteriaQuery :

and some joins may be added to the root until the query have been executed:

and the generated exception is like :

> org.hibernate.hql.ast.QuerySyntaxException: Invalid path:
> '' [select count(generatedAlias0) from entity.Entity
> as generatedAlias0 where (>=13L ) and (
> (<=34L ) )]

which generatedAlias1 should be on Entity and generatedAlias0 should be on the association that I joined on that.
Note that I implement Join properly because when I execute query without count query it executes without error and the Join works properly but when I try to execute count query it throws exception.
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!