我不知道如何在休眠中将 sql 查询转换为 criteriabuilder?

I don't know how to convert sql query to criteriabuilder in hibernate?

我需要总预算值。我可以从 sql 查询中得到答案,但问题是如何将 sql 查询转换为 sql 查询下方 hibernate.Here 中的 criteriaBuilder:

SELECT SUM(ab.daily_budget) as todayBudgetTar FROM api_budget ab INNER JOIN api_ad_groups ad on ad.ad_group_id= ab.adgroup_id INNER JOIN api_campaigns c on c.campaign_id =ad.campaign_id INNER JOIN api_user_account ac on ac.user_id=ad.user_id WHERE ad.user_id = 234 AND ad.status=0 AND c.status=1 按 "todayBudgetTar"

分组

如果有人知道请帮助我。

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> query = criteriaBuilder.createQuery(Object[].class);

Root<ApiBudget> budget = query.from(ApiBudget.class);
Root<ApiAdGroups> adGroups = query.from(ApiAdGroups.class);
Root<ApiCampaigns> campaigns = query.from(ApiCampaigns.class);
Root<ApiUserAccount> userAccount = query.from(ApiUserAccount.class);

//this one is on your entity field name
query.select(criteriaBuilder.sum(budget.get("dailyBudget"));

List<Predicate> predicates = new ArrayList<Predicate>();

predicates.add(
        qb.equal(adGroups.get("adGroupId"), budget.get("adGroupId")));
predicates.add(
        qb.equal(campaigns.get("campaignId "), adGroups.get("campaignId")));
predicates.add(
        qb.equal(userAccount.get("userId"), adGroups.get("userId")));

predicates.add(
        qb.equal(adGroups.get("userId"), 234));

predicates.add(
        qb.equal(adGroups.get("status"), 0));

predicates.add(
        qb.equal(campaigns.get("status"), 1));

query.where(predicates.toArray(new Predicate[]{}));


query.groupBy(budget.get("dailyBudget"));


TypedQuery<Object[]> typedQuery = entityManager.createQuery(query);
List<Object[]> resultList = typedQuery.getResultList();

嗨,我认为这应该有效

CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<Object> query = builder.createQuery(Object.class);
    Root<Budget> budget = query.from(Budget.class);
    Join adsetJoin = budget.join("adsetId");
    Join campaignJoin = adsetJoin.join("campaign");
    query.multiselect(builder.sum(budget.get("dailyBudget")));
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(builder.equal(adsetJoin.get("status"), 0));
    predicates.add(builder.equal(adsetJoin.get("userId"), userId));
    predicates.add(builder.equal(campaignJoin.get("campaignStatus"), CAMPAIGNSTATUS.RUNNING));
    if ((dashboardRequest.getStartDate() != null) && (dashboardRequest.getEndDate() != null)) {
        Date fromDate = new Date(dashboardRequest.getStartDate());
        Date toDate = new Date(dashboardRequest.getEndDate());
        predicates.add(builder.between(adsetJoin.get("createdDate"), fromDate, toDate));
    }
    if (!predicates.isEmpty()) {
        Predicate[] pr = new Predicate[predicates.size()];
        pr = predicates.toArray(pr);
        query.where(pr);
    }
    query.groupBy(budget.get("dailyBudget"));
    List<Object> resultList = session.createQuery(query).getResultList();
    List<Long> resultList2=resultList.stream().map(v ->((Number)v).longValue()).collect(Collectors.toList());
    return resultList2;