我认为我的问题很简单,但我不知道如何解决它。
所以我有一个团队,这个团队可以有一场比赛(一场比赛是两个团队之间的战斗)。当我有一支球队的时候,我也想看看他在打什么比赛。但我有比赛和球队的开始。
matches数组有2个团队,这些团队又有一个matches数组,依此类推。
下面是一个截图来澄清:
我已经尝试将@jsonbackreference放在set matches变量的上方,但这只会隐藏整个数组。那不是我想要的。我想显示matches数组,然后将其隐藏为“teamhome”,如图所示。
控制器和逻辑类对此并不重要,所以我将向您展示实体的代码以及它们之间的关系。
match类:
@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”})。这可以防止它返回子对象中的matches数组。
我编辑了代码。
暂无答案!
目前还没有任何答案,快来回答吧!