父组合键的JPA注释将成为子组合主键的一部分 [英] JPA annotation for parent composite key to be part of child composite primary key
问题描述
我正在尝试绘制具有多个比赛的比赛日,并且无法找出注释.
I am trying to map a race day that has multiple races, and can't figure out the annotations.
我最终想要得到的是这样的东西(这是一个简化的示例):
What I want to end up with is something like (this is a simplified example):
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 +注释:
So far I've come up with the following POJO + annotations:
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示例,但似乎没有一个可以按照我需要的方式将两者结合在一起.
How do I make an EmbeddedId for Race (I'm guessing that's what I need) with both the raceDay join column AND raceNumber in it? Any help appreciated. I have seen dozens of JoinColumn examples and EmbeddedId examples here on StackOverflow but none that seem to combine the two in the way that I need.
推荐答案
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
对于上述给定的表,我可以使用以下代码生成序列并按照约束将数据持久保存在表中.
For the above given tables, I was able to generate the sequence and get the data persisted in the tables following the constraints using the below code.
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代码进行一些更改.
the dao code needs to be changed a bit as to below.
public RaceDay saveRaceDay(RaceDay raceDay){
if(raceDay.getRaces()!=null){
raceDay.getRaces().forEach(race ->{
race.setRaceDay(raceDay);
});
}
}
这篇关于父组合键的JPA注释将成为子组合主键的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!