11

如何使用 Spring Data JPA 进行不同的计数?我不想使用@Query注释。等效的 sql 查询将是:

SELECT COUNT(DISTINCT rack) FROM store WHERE id = 10001;

我尝试编写以下查询,但它不起作用:

int countDistinctRackById();
4

3 回答 3

7

这应该工作:

Integer countDistinctRackById(Long id);
于 2018-06-04T11:06:41.520 回答
5

根据Spring Documentation试试这个:

Integer countDistinctRackById(String id);

将单词替换Rack为机架字段的实际名称

于 2018-06-04T11:06:59.903 回答
1

我刚刚尝试过,现有的答案都不起作用。

以下查询:

Integer countDistinctRackById(String id);

将呈现如下内容:

select distinct count(table0_.rack) as col_0_0_ from table table0_ where table0_.id=?

由于我目前坚持使用 Spring Boot 1.2.3,如果以后有任何更改,请注意,但是为了实现不同的计数,我必须定义一个自定义查询:

@Query("Select count(distinct rack) from Table t where t.id = ?1")

于 2018-10-09T05:12:58.557 回答