Select Distinct * as query method?

2019-08-11 03:33发布

In my Spring project, I'm currently using only query methods.

Now, when calling findAll(Pageable) with a pageable that contains a sort of a collection property, I'm experiencing a known and expected issue:

DATAJPA-744: duplicate results when sorting by collection property

An easy way and also suggested way to solve this is by using the DISTINCT keyword to filter the result.

My problem is that when I create the repository method findDistinct, spring throws an exception on initializing telling me

No property findDistinct found for type RoleEntity

My actual code:

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

From the documentation I would have expected this to work: SpringDocs: query-methods

So my question:

Is it possible to define a Select DISTINCT * with a query method at all?

1条回答
forever°为你锁心
2楼-- · 2019-08-11 03:46

The List returned is already distinct as per the primary key. You must use FindDistinctBy some column name.

查看更多
登录 后发表回答