我正在尝试绘制一个有多场比赛的比赛日,并且无法找出注释。
我最终想要的是(这是一个简化的例子):
TABLE: race_day
date (PK)
venue (PK)
description
TABLE: race
date (PK but also FK on race_day table)
venue (PK but also FK on race_day table)
race_number (PK)
description
到目前为止,我已经提出了以下POJO +注释:
public class RaceDayPK implements Serializable {
@Column(name="race_date")
private String raceDate;
@Column(name="venue")
private String venue;
...
}
public class RaceDay {
@EmbeddedId
private RaceDayPK raceDayPk;
@OneToMany(mappedBy = "raceDay", orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private final List<Race> races = new ArrayList<>();
...
}
public class Race {
@ManyToOne
@JoinColumns({
@JoinColumn(name = "race_date", referencedColumnName = "race_date"),
@JoinColumn(name = "venue", referencedColumnName = "venue")
})
private RaceDay raceDay;
private int raceNumber;
private String raceTitle;
...
}
我如何使用raceDay连接列和raceNumber为Race制作EmbeddedId(我猜这是我需要的)?任何帮助赞赏。我在StackOverflow上看到过几十个JoinColumn示例和EmbeddedId示例,但似乎没有一个以我需要的方式将两者结合起来。
答案 0 :(得分:1)
经过一番黑客攻击后,我认为我得到了这样的工作。请注意OneToMany批注的mappedBy参数中嵌套的“racePk.raceMeeting”:
public class RaceDayPK implements Serializable {
@Column(name="race_date")
private String raceDate;
@Column(name="venue")
private String venue;
...
}
public class RaceDay {
@EmbeddedId
private RaceDayPK raceDayPk;
@OneToMany(mappedBy = "racePk.raceDay", orphanRemoval = true, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private final List<Race> races = new ArrayList<>();
...
}
public class RacePk implements Serializable {
@ManyToOne
@JoinColumns({
@JoinColumn(name = "race_date", referencedColumnName = "race_date"),
@JoinColumn(name = "venue", referencedColumnName = "venue")
})
private RaceDay raceDay;
@Column(name = "race_number")
private int raceNumber;
...
}
public class Race {
@EmbeddedId
private final RacePk racePk = new RacePk();
private String raceTitle;
...
}
不知道这是否会在某些时候失败,但MySQL表似乎是自动生成的。
答案 1 :(得分:1)
TABLE: race_day
id(PK)
date (PK)
venue (PK)
description
TABLE: race
id(PK but also FK on race_day table)
date (PK but also FK on race_day table)
venue (PK but also FK on race_day table)
race_number (PK)
description
对于上面给定的表,我可以使用以下代码生成序列并按照约束将数据持久保存在表中。
public class RaceDayPK implements Serializable {
private Long id;
private String raceDate;
private String venue;
...
}
@Entity
@IdClass(RaceDayPK.class)
public class RaceDay {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "R_SEQ")
@SequenceGenerator(sequenceName = "RD_SEQ", allocationSize = 1, name = "R_SEQ")
private Long id;
@Id
@Column(name="race_date")
private String raceDate;
@Id
@Column(name="venue")
private String venue;
@OneToMany(mappedBy = "raceDay", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<Race> races;
...
}
public class RacePK implements Serializable {
private RaceDay raceDay;
private int raceNumber;
...
}
@Entity
@IdClass(RacePK.class)
public class Race {
@Id
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "race_date", referencedColumnName = "race_date"),
@JoinColumn(name = "venue", referencedColumnName = "venue"),
@JoinColumn(name = "id", referencedColumnName = "id")
})
@JsonIgnore
private RaceDay raceDay;
@Id
private int raceNumber;
private String raceTitle;
...
}
下面需要对dao代码进行一些更改。
public RaceDay saveRaceDay(RaceDay raceDay){
if(raceDay.getRaces()!=null){
raceDay.getRaces().forEach(race ->{
race.setRaceDay(raceDay);
});
}
}
答案 2 :(得分:0)
不确定它是否有效但尝试这种方法:
public class RacePK implements Serializable {
private RaceDayPK raceDay;
private String raceNumber;
...
}
@Entity
@IdClass(RacePK.class)
public class Race {
@Id
@ManyToOne...
private RaceDay raceDay;
@Id
private int raceNumber;
private String raceTitle;
...
}
请注意,PK字段名称应与@ id-annotaed字段名称完全相同。 Google for Derived标识符可了解详情。