我正在开发一个休闲羽毛球联赛的门户网站(注册和分析比赛统计数据)。昨天,当我开始实现一个显示团队统计数据的视图(例如打/赢/输的比赛......)时,我发现了一个神秘的漏洞。
当我用查询检索所有球队时,应该包含在客场比赛的系列赛的字段也包括在主场比赛的系列赛。但是,包含在主场进行的系列赛的字段不包含在客场进行的系列赛(这是我想要的行为)。
此外,如果我再次执行查询(更新页面),包含客场比赛系列的字段开始增长。每次我执行查询时,主场比赛集都会在 awaySeries 字段中再次注册。
例如:
第一个查询:
团队1
gameSeriesAway:Team1 vs Team2,Team1 vs Team3
gameSeriesHome:Team1 vs Team3
第二个查询:
团队1
gameSeriesAway:Team1 vs Team2,Team1 vs Team3,Team1 vs Team3
gameSeriesHome:Team1 vs Team3
团队.java
@Entity
@NamedQueries({@NamedQuery(name = "Team.findAll", query="SELECT distinct t FROM Team t")})
public class Team implements Comparable<Team> {
public static final String FIND_ALL_TEAMS = "Team.findAll";
//Fields
@Id
private String name;
private String description;
//Relationships
@OneToMany(mappedBy = "team")
private List<Player> players;
@ManyToMany
private List<Division> divisionHistory;
@OneToMany(mappedBy="homeTeam")
private List<GameSeries> homeSeries;
@OneToMany(mappedBy="awayTeam")
private List<GameSeries> awaySeries;
...
...
@Override
public boolean equals(Object obj) {
if(obj instanceof Team)
return name.equals(((Team) obj).name)? true:false;
return false;
}
@Override
public int hashCode() {
return name.hashCode();
}
}
游戏系列.java
@Entity
public class GameSeries {
public static final int NUMBER_OF_GAMES = 5;
// Fields
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private Timestamp playDate;
// Relationships
@OneToMany(mappedBy = "series")
private List<Game> games;
@ManyToOne
private Division division;
@ManyToOne
private Team homeTeam;
@ManyToOne
private Team awayTeam;
@Override
public boolean equals(Object obj) {
if (obj instanceof GameSeries)
return playDate.equals(((GameSeries) obj).playDate)
&& homeTeam.equals(((GameSeries) obj).homeTeam)
&& awayTeam.equals(((GameSeries) obj).awayTeam)
? true : false;
return false;
}
@Override
public int hashCode() {
return playDate.hashCode() + 2*homeTeam.hashCode() + awayTeam.hashCode();
}
}
对应的表格如下所示:
游戏系列
id PRIM
播放日期
awayTeam_name MUL
Division_id MUL
homeTeam_name MUL
团队
名称 PRIM
描述
我有一种预感,这是一个非常容易解决的问题,但是由于我是 JPA/EJB 的新手,所以我无法弄清楚这个问题