首页 > 解决方案 > Spring Boot JPA - 返回带有列表的自定义对象

问题描述

我正在使用 Spring JpaRepository 开发一个 Spring Boot 应用程序。我正在使用自己的查询返回自定义对象列表,这些对象是具有 ID、名称和团队列表的组。

所以我有这样的事情:

public interface GroupDB {
    public Long getId();
    public String getName();
    public List<TeamDB> getTeams();
}
public interface TeamDB {
    public Long getId();
    public Long getWon();
    public Long getDrawn();
    public Long getLost();
    public Long getPoints();
}

我的查询返回这个(我试图自己解决我的问题,这就是为什么这里是“团队点......”但这不起作用):

SELECT
        id,
        name,
        team AS "team.id",
        SUM(win) AS "team.won",
        SUM(draw) AS "team.drawn",
        SUM(loss) AS "team.lost",
        3*SUM(win)+SUM(draw) AS "team.points"
...

查询非常非常长,但如果你想要它,这里是:https ://pastebin.pl/view/45b082f5

然后我向数据库发出请求。

@Query(value = getGroupsFromGroupsStageQuery, nativeQuery = true)
List<GroupDB> getGroupsFromGroupsStage(@Param("tournament_id") Long tournamentId);

举个例子:
我想得到一组id(8905),名字(“Grupa 65”)和四支球队的名单:id,win,drawn,lost和points。
但我能得到的最好的东西是 4 个组的列表,其中包含 id、名称和null分配给团队

例子

那么我该怎么做呢?

标签: javapostgresqlspring-bootspring-data-jpa

解决方案


推荐阅读