nvalid路径:“ generatedAlias1.user” [从com.bbc.domain.User中选择count(generatedAlias0)作为generateAlias0,其中generateAlias1.user = 291L]

时间:2019-06-18 15:08:27

标签: jpa spring-data-jpa persistent

无效的路径:'generatedAlias1.user'[从com.bbc.domain.User中选择count(generatedAlias0)作为generateAlias0,其中generateAlias1.user = 291L]

Map finalData = new HashMap <>();         PageRequest request = new PageRequest(customerBalanceDTO.getPageNumber()-1,customerBalanceDTO.getPageSize(),Sort.Direction.ASC,“ id”);

    /*
     *Adding filter start here
     */
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<User> criteria = builder.createQuery(User.class);

    Root<User> userRoot = criteria.from(User.class);
    Root<UserWallet> userWalletRoot = criteria.from(UserWallet.class);
    //Added join to get common users
    Join<UserWallet, User> userWalletUserJoin = userWalletRoot.join(UserWallet_.user);

    //Added distinct to remove duplicate user from the list
    criteria.select(
        builder.construct(User.class,
            userWalletUserJoin.get(User_.firstName),
            userWalletUserJoin.get(User_.lastName),
            userWalletUserJoin.get(SqlBaseEntity_.id)
        )
    ).distinct(true);

    List<Predicate> predicates = new ArrayList<Predicate>();
    Predicate findByUserId = null;
    Predicate findByWalletType = null;

    //Search by user id
    if(Optional.ofNullable(customerBalanceDTO.getUserId()).isPresent() && customerBalanceDTO.getUserId() > 0) {
        findByUserId = builder.equal(userWalletRoot.get(UserWallet_.user), Long.valueOf(customerBalanceDTO.getUserId()));
        predicates.add(findByUserId);
    }

    if(Optional.ofNullable(customerBalanceDTO.getWalletType()).isPresent()){
        logger.info("--------findByWalletType");
        findByWalletType = builder.and(builder.equal(userWalletRoot.get(UserWallet_.walletType), customerBalanceDTO.getWalletType()),
                builder.greaterThan(userWalletRoot.get("balance"), 0));
        predicates.add(findByWalletType);
    }

    criteria = criteria.where(predicates.toArray(new Predicate[]{})).orderBy(builder.asc(userWalletRoot.get(UserWallet_.user)));
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    Root<User> entity_ = countQuery.from(criteria.getResultType());
    countQuery.select(builder.count(entity_));
    Predicate restriction = criteria.getRestriction();
    if (Optional.ofNullable(restriction).isPresent()) {
        countQuery.where(restriction);
    }
    finalData.put("count", entityManager.createQuery(countQuery).getSingleResult());
    List<User> userList = entityManager.createQuery(criteria).setFirstResult(request.getOffset()).setMaxResults(customerBalanceDTO.getPageSize()).getResultList();

0 个答案:

没有答案