简体   繁体   English

JPA返回HashMap <Integer,ArrayList<Integer> &gt;使用本机或命名查询

[英]JPA return HashMap<Integer,ArrayList<Integer>> using Native or Named Query

i have a table with the folowing structure : 我有一个具有以下结构的表:

ID   id_client   id_order

1      1          10
2      1          15
3      1          13
4      2          20
5      2          40
6      2          9

my issue is : how to create a query(named or native) that return the result as 我的问题是:如何创建一个查询(命名或本机)以返回结果

HashMap<Integer,ArrayList<Integer>>

my map contains a unique id_client with an arrayList of all id_order: 我的地图包含一个唯一的id_client,其中包含所有id_order的arrayList:

1=>10,15,13

2=>20,40,9

note ; 注意 ; i'm using EclipseLink as a JPA implementation 我正在使用EclipseLink作为JPA实现

thanks in advance. 提前致谢。

this is how to deal with this issue , you should use Stream API. 这是处理此问题的方法,应使用Stream API。

 List<Entity> listEntity = yourservicenameTogetDataFromDataBase();

//grouping By id_client 
Map<Integer, List<Integer>> groupingMap = listEntity
      .stream()
   .collect(Collectors.groupingBy(o->o.getIdClient()));

//result

for (Entry<Integer, List<Integer>> entry : groupingMap.entrySet()) {
        System.err.println("Key = " + entry.getKey() +" , Values :");
          for (Integer  i : entry.getValue()) {
      System.err.println(i);

      }
  }

Key = 1 , Values :
  10
  15
  13

Key = 2 , Values :

  20
  40
  9
.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM