title: JPA many to many Insert
body: <p>I have 3 tables. 1 is parent, 1 is student and one is student_parent. This is a many to many relationship between the parent and student. The parentID is from the parent table and the studentID is from the student table. The student_parent table have studentID, parentID and childRelationship. I need to know how to update the childRelationship field using JPA(hibernate).</p>
  
  <p>Note that I'm using Java Spring with the JPA/Hibernate to update my relational database and that there are several other tables but all I wanted to know was to update childRelationship field.</p>
  
  <pre><code>@Entity
  @Table(name = "Student")
  @Proxy(lazy = false)
  public class Student{
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "studentID", nullable = false)
  private int studentID;
  
  
  @Column(name="firstName")
  private String firstName;   
  
  @Column(name="middleName")
  private String middleName;
  
  @Column(name="lastName")
  private String lastName;    
  
  @Column(name="validationID")
  private String validationID;
  
  @Column(name="birthDate")
  private Date birthDate;
  
  @Column(name="gender")
  private char gender;
  
  @Column(name="ethnicity")
  private String ethnicity;
  
  @Column(name="photo") // character type has to confirmed
  private byte[] photo;
  
  @Column(name="gradeLevel")
  private int gradeLevel;
  
  @Column(name="rewardPoints")
  private String rewardPoints;
  
  @Column(name="dateCreated")
  private Date dateCreated;
  
  @Column(name="schoolID")
  private Integer schoolID;
  
     @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinColumn(name = "addressID")
      private Address address;
  
      @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinColumn(name = "userName")
      private UserAccount userAccount;
  
      @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
      @JoinTable(name="student_parent",
              joinColumns = {@JoinColumn(name = "studentID")},
              inverseJoinColumns = {@JoinColumn(name = "parentID")})
      private Set&lt;Parent&gt; parents = new HashSet&lt;Parent&gt;();
  
  
      @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
      @JoinTable(name="student_community",
              joinColumns = {@JoinColumn(name = "studentID")},
              inverseJoinColumns = {@JoinColumn(name = "communityID")})
      private Set&lt;Community&gt; communities = new HashSet&lt;Community&gt;();
  
  
  
      //getter and setting
  </code></pre>
  
  <p>then the parent.</p>
  
  <pre><code>@Entity
  @Proxy(lazy = false)
  public class Parent {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int parentID;   
  
  @Column(name="userName")
  private String userName;
  
  @Column(name="firstName")
  private String firstName;   
  
  @Column(name="middleName")
  private String middleName;
  
  @Column(name="lastName")
  private String lastName;    
  
  @Column(name="validationID")
  private String validationID;
  
  @Column(name="maritalStatus")
  private String maritalStatus;
  
  @Column(name="noOfChildren")
  private int noOfChildren;
  
  @Column(name="birthDate")
  private Date birthDate;
  
  @Column(name="gender")
  private char gender;
  
  @Column(name="ethnicity")
  private String ethnicity;
  
  @Column(name="annualIncome")
  private Float annualIncome;
  
  @Column(name="dateCreated")
  private Date dateCreated;
  
  @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinColumn(name = "addressID")
      private Address address;
  
  @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name = "userName",unique = true, nullable = false, insertable=false, updatable=false)
  private UserAccount userAccount;
  
  //getter and setter
  </code></pre>