Herr Derb
Herr Derb

Reputation: 5387

Select Distinct * as query method?

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?

Upvotes: 1

Views: 1314

Answers (1)

uzairalam94
uzairalam94

Reputation: 11

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

Upvotes: 1

Related Questions