是否可以将COUNT与DISTINCT JPA投影配合使用? [英] Is it possible to use COUNT with a DISTINCT JPA projection?

查看:63
本文介绍了是否可以将COUNT与DISTINCT JPA投影配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JPA非常规投影来获取一些数据:

I am using a JPA distinct projection to get some data:

select distinct o.f1, o.f2, o.f3 from SomeEntity o where ...

这可以很好地将setFirstResult和setMaxResults用于页面数据.

This works fine with setFirstResult and setMaxResults to page data.

但是,我需要计算总行数而不获取所有行.我已经尝试过:

However I need to count the total number of rows without fetching all of them. I have tried:

select count(distinct o.f1, o.f2, o.f3) from SomeEntity o where ...

这不起作用(无论如何对于EclipseLink),并且JPA规范似乎不允许这样做.还有另一种方法吗?我不想写一个SQL查询来做到这一点.

This does not work (with EclipseLink anyway) and it doesn't seem to be allowed by the JPA spec. Is there another way? I don't want to have to write an SQL query to do this.

推荐答案

尝试一下:

select count(distinct o) from SomeEntity o where ...

这篇关于是否可以将COUNT与DISTINCT JPA投影配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆