如何对值进行分组和连接?

我有这个方法:


 public List<IncomeChannelCategoryMap> allIncomeChannels(final List<String> list) {


        final CriteriaQuery<IncomeChannelCategoryMap> criteriaQuery = builder.createQuery(IncomeChannelCategoryMap.class);

        final Root<IncomeChannelMapEntity> root = criteriaQuery.from(IncomeChannelMapEntity.class);


        final List<Selection<?>> selections = new ArrayList<>();

        selections.add(root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code));

        selections.add(root.get(IncomeChannelMapEntity_.logicalUnitCode));

        selections.add(root.get(IncomeChannelMapEntity_.logicalUnitIdent));

        selections.add(root.get(IncomeChannelMapEntity_.keyword));

        criteriaQuery.multiselect(selections);

        Predicate codePredicate = root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code).in(list);

        criteriaQuery.where(codePredicate);

        return entityManager.createQuery(criteriaQuery).getResultList();


    }

响应是这样的:


[

{

    "incomeChannelCode": "DIRECT_SALES",

    "logicalUnitCode": "R_CATEGORY",

    "logicalUnitIdent": "7"

  },

  {

    "incomeChannelCode": "DIRECT_SALES",

    "logicalUnitCode": "R_CATEGORY",

    "logicalUnitIdent": "8"

  }

]

我想要实现的是:


  {

    "incomeChannelCode": "DIRECT_SALES",

    "logicalUnitCode": "R_CATEGORY",

    "logicalUnitIdent": "7,8"

  }

有什么建议我怎样才能实现这个目标?


我尝试过这个,我在一些例子中发现了这一点:


builder.function("group_concat", String.class, root.get(IncomeChannelMapEntity_.logicalUnitIdent));

但这是行不通的。还有其他建议吗?


@Data

@Builder

@NoArgsConstructor

@AllArgsConstructor

public class IncomeChannelCategoryMap implements Serializable {

    private static final long serialVersionUID = 1L;


    private String incomeChannelCode;

    private String logicalUnitCode;

    private String logicalUnitIdent;

    private String keyword;


}


RISEBY
浏览 123回答 2
2回答

凤凰求蛊

尝试这个:ArrayList<IncomeChannelCategoryMap> list = entityManager.createQuery(criteriaQuery).getResultList();List<IncomeChannelCategoryMap> finalList = new ArrayList<>(list.stream().collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collectors.toMap(IncomeChannelCategoryMap::getIncomeChannelCode, Function.identity(), (IncomeChannelCategoryMap i1, IncomeChannelCategoryMap i2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i1.setLogicalUnitIdent(i1.getLogicalUnitIdent()+","+i2.getLogicalUnitIdent());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return i1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})).values());&nbsp;return finalList;注意:请相应地添加您的 getter 方法,我只是假设您有这些方法名称。

慕后森

您应该使用本机查询方法,正如我在上一个问题中建议的那样:public List<IncomeChannelCategoryMap> allIncomeChannels(final List<String> list) {&nbsp; &nbsp; List<Object[]> resultList = entityManager.createNativeQuery(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "select income_channel_code, logicalunit_code, string_agg(logicalunitident,',') idents, keyword from r_income_channel_map where income_channel_code in (:codes) group by logicalunit_code, income_channel_code, keyword")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setParameter("codes", list).getResultList();&nbsp; &nbsp; return resultList.stream().map(IncomeChannelCategoryMap::new).collect(Collectors.toList());}您需要将此构造函数添加到您的IncomeChannelCategoryMap类中:IncomeChannelCategoryMap(Object[] objects) {&nbsp; &nbsp; this.incomeChannelCode = (String) objects[0];&nbsp; &nbsp; this.logicalUnitCode = (String) objects[1];&nbsp; &nbsp; this.logicalUnitIdent = (String) objects[2];&nbsp; &nbsp; this.keyword = (String) objects[3];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java