2

Can you help me connect Specifications and Query, Pageable works perfectly but I have a problem with Specifications, no search parameters are added to where.

public interface RakebackRepository extends JpaRepository<Rakeback, Long>,JpaSpecificationExecutor<Rakeback> {

    @Override    
    @Query(value = "SELECT new domain.model.poker.rakeback.Rakeback(ppr.playerId, SUM(ppr.rakeSum), SUM(ppr.fullRakeBackSum), max(ppra.rakeBackBalance), max(ppra.rakebackRank)) FROM Rakeback as ppr LEFT JOIN ppr.Rakeback as ppra on ppra.status = 'active' LEFT JOIN ppra.rakebackRank as rbb on ppra.rakeBackRankId = rbb.id GROUP BY ppr.playerId ")   
    Page<Rakeback> findAll(Specification<Rakeback> specification, Pageable pageable);
    
    }

Here is SQL from logs

select pokerplaye0_.player_id as col_0_0_,  sum(pokerplaye0_.rake_sum) as col_1_0_,  sum(pokerplaye0_.full_rake_back_sum) as col_2_0_,  max(pokerplaye1_.rake_back_balance) as col_3_0_,  max(pokerplaye1_.rake_back_rank_id) as col_4_0_ 
from rakeback.players_rakebacks pokerplaye0_  
left outer join rakeback.players_rakebacks pokerplaye1_ on pokerplaye0_.player_id=pokerplaye1_.player_id and (pokerplaye1_.status='active')  
left outer join rakeback.rakeback_settings pokerrakeb2_ on pokerplaye1_.rake_back_rank_id=pokerrakeb2_.id and (pokerplaye1_.rake_back_rank_id=pokerrakeb2_.id) 
group by pokerplaye0_.player_id 
order by pokerplaye0_.player_id desc limit ?

Here is my Speec

public class RakebackListSpec {

private final OperatorService operatorService;
private final PlayerService playerService;



public static Specification<Rakeback> byPlayerId(Long id) {
    return (root, query, criteriaBuilder) ->
            criteriaBuilder.equal(root.get("playerId"), id);
}

public static Specification<Rakeback> byOperator(Integer operatorId) {
    return (root, query, criteriaBuilder) ->
            criteriaBuilder.equal(root.get("operatorId"), operatorId);
}

public Specification<Rakeback> getSpec(RakebackListRequestDto request) {

    Specification<Rakeback> spec = Specification.where(byOperator(operatorService.getOperator().getId()));

    if(request.getOpPlayerId() != null){
        CorePlayer player = playerService.getPlayerByOpPlayerId(request.getOpPlayerId());
        spec = spec.and(byPlayerId(player.getId()));
    }


    return spec;
}

1 Answer 1

5

@Query and Specification cannot be combined. See this SO Question/Answer for details.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe there are some alternative options for generating dynamic where in the query?
Yes, Specification. Have you read up on it in the Spring Data documentation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.