Thursday, 5 July 2007

Getting a query count of records in EJB3

I was working on a small pagination demo with EJB3 and JDeveloper 10.1.3.1. I thought creating a query to count the number of records in my result set would be as simple as "select count(*)" but in EJB3 and JPQL world you can't use "select count(*)" unless you create a native query. Here is my named queries I used to get a count of my result set. In JPQL it's done with a query as follows, so no need to use a native query after all.

select COUNT(o) from AllObjectsRo o

@NamedQueries({@NamedQuery(name="AllObjectsRo.countAll",
query="select COUNT(o) from AllObjectsRo o"),
@NamedQuery(name="AllObjectsRo.findAll",
query="select o from AllObjectsRo o")
})

No comments: