有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导返回嵌套数组,但是。。。我需要数据,但只需要一次

我认为我的问题很简单,但我不知道如何解决它

所以我有一个团队,这个团队可以有一场比赛(一场比赛是两个团队之间的战斗)。当我得到一支球队时,我也希望看到他正在进行的比赛。但我有一个比赛和团队的开端

The matches array has 2 teams, Those teams again have a matches array and so on.

下面是一个屏幕截图来澄清它:Matches array example 我已经尝试将@JsonBackReference放在Set matches变量之上,但这只会隐藏整个数组。那不是我想要的。我想显示matches数组,然后将其隐藏为“teamHome”,如图所示

控制器和逻辑类对此并不重要,因此我将向您展示实体的代码以及它们之间的关系

比赛级别:

@Entity
@Getter
@Setter
@Table(name="match")
public class Match {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    @JoinColumn(name = "team_id_home")
    @JsonIgnoreProperties({"matches"})
    private Team teamHome;

    @ManyToOne
    @JoinColumn(name = "team_id_away")
    @JsonIgnoreProperties({"matches"})
    private Team teamAway;

    private int scoreTeamHome;

    private int scoreTeamAway;

    private int nrOfWinsNeeded;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern="yyyy-MM-dd HH:mm")
    private Date date;

    public Match() {
    }

    public Match(Team teamHome, Team teamAway, int scoreTeamHome, int scoreTeamAway, int nrOfWinsNeeded, Date date){
        this.teamHome = teamHome;
        this.teamAway = teamAway;
        this.scoreTeamHome = scoreTeamHome;
        this.scoreTeamAway = scoreTeamAway;
        this.nrOfWinsNeeded = nrOfWinsNeeded;
        this.date = date;
    }


}

小组课:

@Entity
@Getter
@Setter
@Table(name = "`team`")
public class Team {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;

   @Column(nullable = false)
   private String name;

   @Column(nullable = false)
   private String country;

   @Column(nullable = false, name = "code_name")
   private String codeName;

   @JsonBackReference
   @OneToMany(cascade = CascadeType.ALL)
   @JoinColumn(name = "team_id") // we need to duplicate the physical information
   private Set<Match> matches;


//    @OneToMany
//    @JoinColumn(name = "team_id") // we need to duplicate the physical information
//    private Set<Player> players;

//  Make sure the player array is not shown in the team
   @JsonBackReference
   @OneToMany(mappedBy = "team")
   private Set<Player> players;

   public Team(){

   }


   public Team(String name, String country, String codeName) {
       this.name = name;
       this.country = country;
       this.codeName = codeName;
   }


}

编辑-我修复了它 我补充说 @teamHome和teamAway上方的JsonIgnoreProperties({“matches”})。这可以防止它返回子对象中的匹配数组

我编辑了代码


共 (0) 个答案