use of getJPAQueryFactory and getJPAQuery

question:

getJPAQueryFactory use with getJPAQuery?
JPAQueryFactory
JPAQuery//Use this first

Eg:

public QueryResults<CardParameterTemplate> getCardType(Pageable pageable, CardParameterTemplateQuery cardTypeQuery) {
        JPAQuery jpaQuery = cardTypeDao.getJPAQuery();
        BooleanBuilder booleanBuilder = buildPredicate(cardTypeQuery);
        jpaQuery.select(QCardParameterTemplate.cardParameterTemplate)
                .from(QCardParameterTemplate.cardParameterTemplate)
                .where(booleanBuilder)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize());
        return jpaQuery.fetchResults();
    }

    @Override
    public HashMap<String, Object> getCardParameterByCardType(String typeName) {
        JPAQueryFactory jpaQueryFactory = cardParameterInfoDao.getJPAQueryFactory();
        List<CardParameterInfo> cardParameterInfoList = jpaQueryFactory.select(QCardParameterInfo.cardParameterInfo)
                .from(QCardParameterInfo.cardParameterInfo)
                .where(QCardParameterInfo.cardParameterInfo.cardParameterId.in(
                        jpaQueryFactory.select(QCardParameter.cardParameter.id)
                                .from(QCardParameter.cardParameter)
                                .where(QCardParameter.cardParameter.cardTypeId.in(
                                        jpaQueryFactory.select(QCardParameterTemplate.cardParameterTemplate.id)
                                                .from(QCardParameterTemplate.cardParameterTemplate)
                                                .where(QCardParameterTemplate.cardParameterTemplate.templateName.eq(typeName))
                                                .fetch()
                                )).fetch()
                )).fetch();

        List<CardParameter> cardParameterList = jpaQueryFactory.select(QCardParameter.cardParameter)
                .from(QCardParameter.cardParameter)
                .where(QCardParameter.cardParameter.cardTypeId.in(
                        jpaQueryFactory.select(QCardParameterTemplate.cardParameterTemplate.id)
                                .from(QCardParameterTemplate.cardParameterTemplate)
                                .where(QCardParameterTemplate.cardParameterTemplate.templateName.eq(typeName))
                                .fetch()
                )).fetch();

        List<Long> groupIdList = jpaQueryFactory.select(QCardParameterInfo.cardParameterInfo.groupId)
                .from(QCardParameterInfo.cardParameterInfo)
                .where(QCardParameterInfo.cardParameterInfo.cardParameterId.in(
                        jpaQueryFactory.select(QCardParameter.cardParameter.id)
                                .from(QCardParameter.cardParameter)
                                .where(QCardParameter.cardParameter.cardTypeId.in(
                                        jpaQueryFactory.select(QCardParameterTemplate.cardParameterTemplate.id)
                                                .from(QCardParameterTemplate.cardParameterTemplate)
                                                .where(QCardParameterTemplate.cardParameterTemplate.templateName.eq(typeName))
                                                .fetch()
                                )).fetch()
                )).groupBy(QCardParameterInfo.cardParameterInfo.groupId)
                .fetch();

        HashMap<String, Object> objectHashMap = new HashMap<>();
        List<Map<String, String>> mapList = new ArrayList<>();
        for (int z = 0; z < groupIdList.size(); z++) {
            HashMap<String, String> hashMap = new HashMap<>();
            for (int i = 0; i < cardParameterInfoList.size(); i++) {
                Long cardParameterId = cardParameterInfoList.get(i).getCardParameterId();
                String value = cardParameterInfoList.get(i).getValue();
                Long groupId = cardParameterInfoList.get(i).getGroupId();
                //1, {MatchTrack=2,MatchOffset=0}
                //2, {MatchTrack=3,MatchOffset=1}
                if (groupId == groupIdList.get(z)) {
                    for (int j = 0; j < cardParameterList.size(); j++) {
                        String name = cardParameterList.get(j).getName();
                        Long id = cardParameterList.get(j).getId();
                        if (name.equals("FITVersion")) {
                            if (id.equals(cardParameterId)) {
                                objectHashMap.put(name, value);
                            }
                        } else {
                            if (id.equals(cardParameterId)) {
                                hashMap.put(name, value);
                            }
                        }
                    }
                }
            }
            mapList.add(hashMap);
        }
        objectHashMap.put("RecordsSupported", mapList);
        return objectHashMap;
    }

    private BooleanBuilder buildPredicate(CardParameterTemplateQuery cardTypeQuery) {
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        if (!StringUtils.isEmpty(cardTypeQuery.getTemplateName())) {
            booleanBuilder.and(QCardParameterTemplate.cardParameterTemplate.templateName.like(cardTypeQuery.getTemplateName().concat("%")));
        }
        return booleanBuilder;
    }

Leave a Comment

Your email address will not be published. Required fields are marked *