选择Distinct *作为查询方法?

时间:2018-01-31 11:13:13

标签: java spring spring-data-jpa distinct jpql

在我的Spring项目中,我目前只使用查询方法。

现在,当使用包含某种集合属性的分页符调用findAll(Pageable)时,我遇到了一个已知且预期的问题:

DATAJPA-744: duplicate results when sorting by collection property

通过使用DISTINCT关键字过滤结果,一种简单的方法以及建议的解决方法。

我的问题是,当我创建存储库方法findDistinct时,spring会在初始化时抛出异常告诉我

  

找不到类型RoleEntity

的属性findDistinct

我的实际代码:

@Repository
public interface RoleRepository extends JpaRepository<RoleEntity, Long>, JpaSpecificationExecutor<RoleEntity>{
    Page<RoleEntity> findDistinct(Specification<RoleEntity> entitySpecification, Pageable pageable);
}

从文档中我预计这将起作用:SpringDocs: query-methods

所以我的问题:

是否可以使用查询方法定义Select DISTINCT *

1 个答案:

答案 0 :(得分:1)

根据主键,返回的列表已经是不同的。您必须使用FindDistinctBy一些列名。