Select Distinct * as query method?

大兔子大兔子 提交于 2020-01-05 05:39:16

问题


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:


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



来源:https://stackoverflow.com/questions/48540789/select-distinct-as-query-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!