我整天都被困住了。我有一个向API进行POST的表单,我希望将数据保存到3个表中。
我是后端编码的新手,尤其是数据库,我出于学习目的选择了这个新堆栈,因此,如果您在这里看到任何愚蠢的东西,现在您知道为什么:-)如果您认为我完全不赞成或不赞成我的数据库设计,请允许我知道。
到目前为止,我在Squad和SquadPlayers的两个类中都有此功能。
Squad.java
package com.FUT.track.web.FUTtrackapplication.squads;
import javax.persistence.*;
@Entity
@Table(name="Squad")
public class Squad {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private int squadId;
private String squadName;
private String squadDescription;
private String primaryFormation;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "playerId")
private SquadPlayers squadPlayers;
public Squad() {
}
public Squad(String squadName, String squadDescription, String primaryFormation, SquadPlayers squadPlayers) {
super();
this.squadName = squadName;
this.squadDescription = squadDescription;
this.primaryFormation = primaryFormation;
this.squadPlayers = squadPlayers;
}
public int getSquadId() {
return squadId;
}
public void setSquadId(int squadId) {
this.squadId = squadId;
}
public String getSquadName() {
return squadName;
}
public void setSquadName(String squadName) {
this.squadName = squadName;
}
public String getSquadDescription() {
return squadDescription;
}
public void setSquadDescription(String squadDescription) {
this.squadDescription = squadDescription;
}
public String getPrimaryFormation() {
return primaryFormation;
}
public void setPrimaryFormation(String primaryFormation) {
this.primaryFormation = primaryFormation;
}
public SquadPlayers getSquadPlayers() {
return squadPlayers;
}
public void setSquadPlayers(SquadPlayers squadPlayers) {
this.squadPlayers = squadPlayers;
}
}
SquadPlayers.java
package com.FUT.track.web.FUTtrackapplication.squads;
import javax.persistence.*;
@Entity
@Table(name="SquadPlayers")
public class SquadPlayers {
@Id
private Integer playerId;
private Integer squadId;
private Integer genericPlayerId;
@OneToOne(mappedBy = "squadPlayers")
private Squad squad;
public Integer getPlayerId() {
return playerId;
}
public void setPlayerId(Integer playerId) {
this.playerId = playerId;
}
public Integer getSquadId() {
return squadId;
}
public void setSquadId(Integer squadId) {
this.squadId = squadId;
}
public Squad getSquad() {
return squad;
}
public void setSquad(Squad squad) {
this.squad = squad;
}
public Integer getGenericPlayerId() {
return genericPlayerId;
}
public void setGenericPlayerId(Integer genericPlayerId) {
this.genericPlayerId = genericPlayerId;
}
}
最佳答案
假设这就是您要执行数据流的方式
player
列表。并且player
可以属于多个小队squad
来在表单提交上创建player
。并且分配的玩家将具有唯一的标识号。 squadPlayer
都会有playerStat
注意:
Player
和Squad
之间的关系是 ManyToMany SquadPlayer
是Player
和Squad
之间的联接表Player
和SquadPlayer
之间的关系为 OneToMany Squad
和SquadPlayer
之间的关系为 OneToMany 播放器
@Entity
@Table(name = "player")
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String position;
private String country;
private String club;
@OneToMany(mappedBy = "player")
private Set<SquadPlayer> squadSet = new HashSet<>();
....
}
在这里,您的
Player
实体具有squadSet
字段的 OneToMany 关联,该字段描述了一个球员可以被包括在多个阵容中的事实。小队
@Entity
@Table(name="squad")
public class Squad {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
private int id;
private String name;
private String description;
private String primaryFormation;
@OneToMany(mappedBy = "squad", cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private Set<SquadPlayer> playerSet = new HashSet<>();
...
}
此处
Squad
实体具有 OneToMany 与playerSet
字段的关联,描述一个小队可能有多个玩家。请注意,与此处的Player
实体不同, OneToMany 批注定义了Merge
和Persist
的级联类型。这告诉hibernate在持久化Squad时也要持久化此关系。SquadPlayer
@Entity
@Table(name = "squad_player")
public class SquadPlayer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "generic_player_id")
private int genericPlayerId;
@ManyToOne
@JoinColumn(name = "squad_id")
private Squad squad;
@ManyToOne
@JoinColumn(name = "player_id")
private Player player;
@OneToOne(mappedBy = "squadPlayer", orphanRemoval = true, cascade = CascadeType.ALL)
private PlayerStat playerStat;
...
}
只需在此处为
Player
和Squad
映射的另一端以及相应的连接列即可现在,您的
PlayerStat
实体具有与 SquadPlayer 的 OneToOne 关系。删除orphanRemoval=true
后,拥有PlayerStat
将从SquadPlayer
中删除该条目(尽管它是可选的)。我们还为此关系定义了级联规则。PlayerStat
@Entity
@Table(name = "player_stat")
public class PlayerStat {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int performance;
@OneToOne
@JoinColumn(name = "squad_player_id")
private SquadPlayer squadPlayer;
...
}
坚持小组与所有这些关系
请记住,JPA或Hibernate确定与对象图的数据库关系。实体从定义其关联的过程中一直与级联关系保持一致。因此,您可以按照以下流程为
Squad
对象进行适当的关联new Squad()
并使用提供的字段设置所有字段。 player
对象拉入同一笔交易player
并为每个new SquadPlayer()
创建PlayerStat
。设置所有关联的字段以及SquadPlayer
字段。然后将每个Squad
添加到playerSet
对象的Squad
字段中。 如果遵循此流程中的所有内容,则数据库应使用适当的关系填充所有表。
供进一步阅读: