在我的 Spring 项目中,我当前仅使用查询方法。
现在,当使用包含某种集合属性的可分页调用 findAll(Pageable)
时,我遇到了一个已知且预期的问题:
DATAJPA-744: duplicate results when sorting by collection property
解决此问题的一个简单方法和建议方法是使用 DISTINCT
关键字来过滤结果。
我的问题是,当我创建存储库方法findDistinct
时,spring在初始化时抛出异常告诉我
No property findDistinct found for type RoleEntity
我的实际代码:
@Repository
public interface RoleRepository extends JpaRepository<RoleEntity, Long>, JpaSpecificationExecutor<RoleEntity>{
Page<RoleEntity> findDistinct(Specification<RoleEntity> entitySpecification, Pageable pageable);
}
从文档中我预计它会起作用:SpringDocs: query-methods
所以我的问题:
是否可以使用查询方法定义 Select DISTINCT *
?
最佳答案
返回的列表根据主键已经不同。您必须使用 FindDistinctBy
某些列名称。
关于java - 选择 Distinct * 作为查询方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48540789/