java - 选择 Distinct * 作为查询方法?

标签 java spring spring-data-jpa distinct jpql

在我的 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/

相关文章:

java - For-Each 循环不兼容类型

java - package-info.java 是如何工作的?

java - Snakeyaml 命名约定

java - Hibernate 无法确定类型

java - 使用 nativeQuery = true 在 JPA Reposity 中执行 @Query 时出现 ResultSet 错误

java - keycloak:检查旧密码是否匹配,如果是则更改为另一个

java - 组件扫描时如何避免Spring中不需要的bean?

java - 如何将表名添加为 hibernate 模板查询的参数之一

mysql - 如何在 Spring boot JPA 中使用多个 json 数组?

java - Spring data jpa 在非常具体的场景中抛出 TransientObjectException