gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:54:01 25 4
gpt4 key购买 nike

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

给定对象:支架编号:1支架编号:2支架编号:3支架编号:3括号ID:3

需要的结果:支架编号:1支架编号:2括号ID: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 );
}
}

给定对象包括:支架编号:1支架编号:2支架编号:3支架编号:3括号ID:3

所需结果包括:支架编号:1支架编号:2括号ID:3

最佳答案

是否可以选择保留查询原样并替换结果?
类似于以下内容:
(假设你的bracketID是一个整数)

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;

关于java - 需要查询来查找具有谓词和属性的不同值的所有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com