Assuming you want your data stream to execute
- You have a predefined list of
player . And player can belong to several units uniquely - You will create
squad when submitting the form by assigning player . And the designated player will have a unique identification number. - Each
squadPlayer will have a playerStat
Note:
- The relationship between
player and squad ManyToMany - Your
squadPlayer is the connection table between player and squad - So the relationship between
player and squadPlayer OneToMany - And the connection between
squad and squadPlayer OneToMany
Player
@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<>(); .... }
Here, your player object has a OneToMany connection to the squadSet field, which displays the fact that the player can be included in several squads.
Structure
@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<>(); ... }
And here, the squad entity has OneToMany associated with the playerSet field, in which Squad can have multiple players. Note that unlike the player object, the OneToMany annotation here defines the cascading type of Merge and Persist . This indicates hibernation while maintaining this relationship while maintaining 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; ... }
Just here, having the other end of the display for player and squad with the corresponding join column
Your playerStat has a OneToOne relationship with SquadPlayer . If orphanRemoval=true will remove the entry from playerStat when squadPlayer is removed (although not necessary). We also defined cascading rules for this relationship.
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; ... }
Persian squad and all these relationships
Remember that JPA or Hibernate defines your relationship with a database with an object graph. And entities are preserved along with cascading relationships from the definition of their rites. So this is a stream that you can use to create the appropriate attribute for the squad object
- Create
new Squad() and set all the fields with the fields provided. - When you get the player id list from the form, pull those
player objects within one transaction - repeat all these
player and create new SquadPlayer() for each. Define all related fields along with the playerStat field. Then add each squadPlayer to the squad object playerSet . - Finally save the
squad object
If you follow all this flow, your database should populate all the tables with the appropriate relationships.
For further reading: