title: Inserting Data in Multiple Tables in Hibernate
body: <p>I have been stuck on this all day. I have a form making a POST to the API and I want the data to be saved into 3 tables. </p>
  
  <ol>
  <li>Records are saved in one table (<strong>Squad</strong>) that has auto generated Id. At insert in this table I want to read the auto generated Id of records and insert those in a different table (<strong>SquadPlayers</strong>) plus also insert an extra field that was submitted by the form in this 2nd table (<strong>SquadPlayers</strong>: <em>GenericPlayerId</em>). </li>
  <li>Also a bit about what I want to submit from the front end form. I want all info about the squad plus Ids for upto 11 players submitted (these ids are what I will like to save in the field <em>GenericPlayerId</em> filed for <strong>SquadPlayers</strong> table). </li>
  </ol>
  
  <p>I am new to backend coding especially databases and this new stack I picked up for learning purposes so if you are seeing anything silly here, now you know why :-) So if you think I am totally off or wrong with the my database design let me know. 
  <a href="https://i.stack.imgur.com/pnqWd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pnqWd.png" alt="enter image description here"></a></p>
  
  <p>So far I have this in my two classes for Squad and SquadPlayers.</p>
  
  <p><strong>Squad.java</strong></p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p><strong>SquadPlayers.java</strong></p>
  
  <pre><code> 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;
      }
  
  }
  </code></pre>