开发者

JPA Is there a way to do something like SELECT <field>, count(*) FROM <table> GROUP BY <field>

I've been looking in the web for examples on the aggregates like count but 开发者_开发技巧it seems all of them are using the aggregate alone.

SELECT field, count(*) FROM table GROUP BY field

Should have something like:

field.value1, x1
field.value2, x2
....

I'm looking for a pure JPA answer for this one. If not I guess I can then do further queries just for the count part but that seems unefficient.

Any ideas?


I'm not sure I understood the question correctly but doesn't the following JPQL query do what you want:

SELECT p.name, count(p) from Product p group by p.name

You can retrieve the data like this:

List datos=(List)query.getResultList();

where Object[0] is the first field, Object[1] the second and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜