需要查询以查找具有谓词和属性的不同值的所有条目

时间:2019-07-18 07:21:38

标签: java spring-boot jpa criteria-api

我有一个属性为“ bracketID”的java对象。此属性不是唯一的。我需要一个查询,该查询根据设置的谓词和不同的属性“ bracketID”为我提供所有对象。

给出对象: bracketId:1 bracketId:2 bracketId:3 bracketId:3 branchId:3

所需结果: bracketId:1 bracketId:2 branchId:3

重要的是,我仍然可以设置任何谓词来进行过滤。

我是这一切的新手,不知道如何解决

 @Override
    public List<UIJobData> getJobs( final String listType, final String filter, final String sortDirection,
                                    final String sortHeader, final Integer pageIndex, final Integer pageSize )
    {
        final CriteriaBuilder criteriaBuilder = m_entityManager.getCriteriaBuilder();
        final CriteriaQuery<UIJobData> criteriaQuery = criteriaBuilder.createQuery( UIJobData.class );
        final Root<UIJobData> jobData = criteriaQuery.from( UIJobData.class );

        final List<Predicate> predicates = new ArrayList<Predicate>();
        addListTypeToPredicates( listType, criteriaBuilder, jobData, predicates );
        addFilterToPredicates( filter, predicates, criteriaBuilder, jobData );

        criteriaQuery.select( jobData ).where(  predicates.toArray(new Predicate[predicates.size()]) );

        setSortToCriteriaQuery( sortDirection, sortHeader, criteriaBuilder, criteriaQuery, jobData );

        final TypedQuery<UIJobData> query = m_entityManager.createQuery( criteriaQuery );
        setPagination( pageIndex, pageSize, query );

        return query.getResultList();
    }

    private void addListTypeToPredicates( final String listType, final CriteriaBuilder criteriaBuilder, final Root<UIJobData> jobData,
        final List<Predicate> predicates )
    {
        if( listType != null && !listType.isEmpty())
        {
            predicates.add( criteriaBuilder.like( jobData.get( "listType" ), listType ) );
        }
    }

    private void addFilterToPredicates( final String filter, final List<Predicate> predicates, final CriteriaBuilder criteriaBuilder, final Root<UIJobData> jobData )
    {
        if( filter != null && !filter.isEmpty() )
        {
            final Predicate id = criteriaBuilder.like( jobData.get( "idString" ), "%" + filter + "%" );
            final Predicate orderName = criteriaBuilder.like( criteriaBuilder.lower( jobData.get( "orderName" ) ), "%" + filter.toLowerCase() + "%" );

            predicates.add( criteriaBuilder.or( id, orderName ) );
        }
    }

    private void setSortToCriteriaQuery( final String sortDirection, final String sortHeader, final CriteriaBuilder criteriaBuilder,
                               final CriteriaQuery<UIJobData> criteriaQuery, final Root<UIJobData> jobData )
    {
        if( sortDirection != null && !sortDirection.isEmpty() && sortHeader != null && !sortHeader.isEmpty() )
        {
            switch( sortDirection )
            {
                case "asc":
                    criteriaQuery.orderBy( criteriaBuilder.asc( jobData.get( sortHeader ) ) );
                    break;
                case "desc":
                    criteriaQuery.orderBy( criteriaBuilder.desc( jobData.get( sortHeader ) ) );
                    break;
                default:
                    criteriaQuery.orderBy( criteriaBuilder.asc( jobData.get( sortHeader ) ) );
                    break;
            }
        }
        else
        {
            criteriaQuery.orderBy( criteriaBuilder.asc( jobData.get( "id" ) ) );
        }
    }

    private void setPagination( final Integer pageIndex, final Integer pageSize, final TypedQuery<UIJobData> query )
    {
        if( pageIndex != null && pageIndex >= 0 && pageSize != null && pageSize >= 0 )
        {
            query.setFirstResult( ( pageIndex ) * pageSize );
            query.setMaxResults( pageSize );
        }
    }

给出对象包括: bracketId:1 bracketId:2 bracketId:3 bracketId:3 branchId:3

所需结果包括: bracketId:1 bracketId:2 branchId:3

1 个答案:

答案 0 :(得分:0)

是否可以保留查询并替换结果呢?
类似于以下内容:
(假设您的方括号ID是一个整数)

List<UIJobData> list = query.getResultList();

ConcurrentMap<Integer, List<UIJobData>> map = list.parallelStream()
              .collect(Collectors.groupingByConcurrent(UIJobData::getBracketID));

List<UIJobData> result = new ArrayList<UIJobdata>();
for(int i : map.keySet()){
    result.add(map.get(i).get(0));
}

return result;