Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据-自定义查询:计数和列表_Java_Spring_Jpa - Fatal编程技术网

Java Spring数据-自定义查询:计数和列表

Java Spring数据-自定义查询:计数和列表,java,spring,jpa,Java,Spring,Jpa,我很难生成一个对象来根据两个表向我提供数据 表一 表二 客观的 我需要一个对象来告诉我每个用户配置文件的名称和数量 类似于(在JSON中): 最好的解决方案是什么?首先,创建一个用于生成Json的bean: public class ABean { private String name; private Integer profiles; // getters and setters here } 然后,您的spring数据jpa查询将如下所示: SELECT ne

我很难生成一个对象来根据两个表向我提供数据

表一 表二 客观的 我需要一个对象来告诉我每个用户配置文件的名称和数量

类似于(在JSON中):


最好的解决方案是什么?

首先,创建一个用于生成Json的bean:

public class ABean {
    private String name;
    private Integer profiles;

    // getters and setters here
}
然后,您的spring数据jpa查询将如下所示:

SELECT new ABean(p.user.name, count(p.id)) FROM Profile p JOIN p.user GROUP BY p.user.name
[
    {"name":"John", "profiles":1 },
    {"name":"Maria", "profiles":5 },
    {"name":"Victor", "profiles":3 }
]
public class ABean {
    private String name;
    private Integer profiles;

    // getters and setters here
}
SELECT new ABean(p.user.name, count(p.id)) FROM Profile p JOIN p.user GROUP BY p.user.name