如何使用Spring Data JPA从列而不是链接中检索值?

时间:2017-12-07 16:31:18

标签: java spring-data spring-data-jpa spring-data-rest

我正在使用投影来检索与内联团队的匹配列表。

投影:

@Projection(name = "matchInlineTeams", types = { Match.class })
    public interface MatchInlineTeams {

    Team getHomeTeam();
    Long getHomeTeamGoals();
    Long getAwayTeamGoals();
    Team getAwayTeam();
}

我的结果是这些的集合:

{
  "homeTeam" : {
    "teamName" : "Banfield",
    "teamFoundation" : "1896-01-21T03:00:00.000+0000",
    "teamCity" : 73,
    "teamCountry" : "ARG",
    "handler" : { },
    "hibernateLazyInitializer" : { }
  },
  "homeTeamGoals" : 2,
  "awayTeamGoals" : 0,
  "awayTeam" : {
    "teamName" : "Gimnasia (LP)",
    "teamFoundation" : "1887-06-03T03:00:00.000+0000",
    "teamCity" : 76,
    "teamCountry" : "ARG",
    "handler" : { },
    "hibernateLazyInitializer" : { }
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/matches/1"
    },
    "match" : {
      "href" : "http://localhost:8080/matches/1{?projection}",
      "templated" : true
    },
    "goals" : {
      "href" : "http://localhost:8080/matches/1/goals"
    },
    "homeTeam" : {
      "href" : "http://localhost:8080/matches/1/homeTeam"
    },
    "competition" : {
      "href" : "http://localhost:8080/matches/1/competition"
    },
    "matchStadium" : {
      "href" : "http://localhost:8080/matches/1/matchStadium"
    },
    "awayTeam" : {
      "href" : "http://localhost:8080/matches/1/awayTeam"
    }
  }
}

我需要为stats应用程序做很多计算,并且我在前端有逻辑,所以要在两个团队之间建立一个匹配历史记录,我需要发出这个请求,并且需要大约一秒来检索所有内容,很好。

我现在的问题是我想从历史比赛中建立一个表格,因此我无法请求两支球队之间的比赛,我必须要求所有球队参赛的比赛。

无论如何,现在我不能使用它,因为而不是200个匹配,我得到3500作为响应,因此构建响应大约需要20秒。

我猜这是因为API返回所有链接并为每个对象解析两个团队,这很好,但我不需要它。有没有办法让我创建一个投影(或任何其他类),它将返回我的列的文字版本而不是解析对象引用?

我希望我的结果是这样的:

{
  "homeTeam" : 10,
  "homeTeamGoals" : 2,
  "awayTeamGoals" : 0,
  "awayTeam" : 36,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/matches/1"
    },
    "match" : {
      "href" : "http://localhost:8080/matches/1{?projection}",
      "templated" : true
  }
}

构建我的表时,我将调用团队端点来解析团队的名称。

所以考虑到这一点,我真正需要的是让它更快(比如快20倍)。因此,如果这不是正确的道路,我将非常感谢一个建议。

0 个答案:

没有答案