【问题标题】:Spring Data @Query + SpecificationSpring Data @Query + 规范
【发布时间】:2021-03-16 14:49:48
【问题描述】:

你能帮我连接规范和查询吗,Pageable 工作得很好,但是规范有问题,没有在哪里添加搜索参数。

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);
    
    }

这是来自日志的 SQL

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 ?

这是我的演讲

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;
}

【问题讨论】:

    标签: java spring hibernate jpa specifications


    【解决方案1】:

    @QuerySpecification 不能组合使用。详情请见this SO Question/Answer

    【讨论】:

    • 也许有一些替代选项可以在查询中生成动态位置?
    • 是的,Specification。您是否在 Spring Data 文档中阅读过它?
    猜你喜欢
    • 2020-05-20
    • 2016-11-05
    • 1970-01-01
    • 2016-07-22
    • 2016-02-17
    • 2017-01-12
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多