spring-projects / spring-projects/spring-data-commons
Improve Querydsl support in custom repositories [DATACMNS-898]
@odrotbohm is already working on this.
Since Dec 30, 2020.
- Dominant language
- Java
- Stars
- 838
- Forks
- 730
- PR merge metrics
- No merged PRs in 30d
Description
Andrei Ivanov opened DATACMNS-898 and commented
Hi,
As far as I understand from the documentation, working with Querydsl in a repository means that the repository interface should extend QueryDslPredicateExecutor:
public interface SiteAccessRequestsRepository extends PagingAndSortingRepository<SiteAccessRequest, Long>, QueryDslPredicateExecutor<SiteAccessRequest> {
}
This seems a bit weird to me because now the "query" gets created in the service/facade layer:
import com.example.siteaccess.dao.expressions.SiteAccessRequestExpressions;
@Service
public class SiteAccessRequestsFacadeImpl implements SiteAccessRequestsFacade {
@Override
public Page<SiteAccessRequest> getSiteAccessRequests(SiteAccessRequest probe, Pageable pageable) throws BusinessException {
Predicate predicate = SiteAccessRequestExpressions.byExample(probe);
return siteAccessRequestsRepository.findAll(predicate, pageable);
}
}
And this also make integration testing difficult, as this is similar to having a method like findAll(String sql) defined in the repository interface.
So I tried to create a custom repository for this:
interface SiteAccessRequestsRepositoryCustom {
Page<SiteAccessRequest> findByExample(SiteAccessRequest probe, Pageable pageable);
}
public class SiteAccessRequestsRepositoryImpl extends QueryDslRepositorySupport implements SiteAccessRequestsRepositoryCustom {
public SiteAccessRequestsRepositoryImpl() {
super(SiteAccessRequest.class);
}
@Override
public Page<SiteAccessRequest> findByExample(SiteAccessRequest criteria, Pageable pageable) {
Predicate predicate = SiteAccessRequestExpressions.byExample(criteria);
//JPQLQuery<?> countQuery = createQuery(predicate);
return null;
}
}
This is were I got stuck.
When using the QueryDslPredicateExecutor, the findAll(Predicate predicate, Pageable pageable) method is handled by QueryDslJpaRepository.
But QueryDslRepositorySupport has only some basic methods to perform queries.
What I am suggesting is to move some of the methods from QueryDslJpaRepository to QueryDslPredicateExecutor to make this case easier to implement.
Or maybe there already is a better way to implement custom repositories with Querydsl that I haven't seen?
Affects: 1.12.2 (Hopper SR2)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.