我有一个CriteriaQuery
,我在这里需要获取行数,但是当我使用@EmbeddedId
定义了一个复合键时,当Hibernate尝试从数据库中获取错误时,运行SQL:
14:35:16,356 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-2) SQL Error: 909, SQLState: 42000
14:35:16,357 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost/127.0.0.1:8080-2) ORA-00909: invalid number of arguments
生成的SQL Hibernate与下面的SQL相似,其中嵌入式ID的成员位于distinct子句中
select
count(distinct tab_.COLUMN_A,
tab_.COLUMN_B,
tab_.COLUMN_C) as col_0_0_
from
SOME_TABLE tab_
where
-- ...
我的Java代码的简化版本是:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
Root<SomeEntity> root = query.from(SomeEntity.class);
countQuery.select(builder.countDistinct(root));
// ... then add predicates
long totalRowCount = entityManager.createQuery(countQuery).getSingleResult();
是否存在使用具有@EmbeddedId的实体来获取计数的其他(正确)方法?