title: hibernate many to many insert only student and mapping tables
body: <p>I have student, test and student_test tables with many-to-many relationship.</p>
  
  <p>Test:</p>
  
  <pre><code>@Entity
  @Table(name = "TEST")
  public class Test 
  {
      @Id
      @Column(name = "TEST_ID")
      @GeneratedValue
      private Long testId;
  
      @Column(name = "SUBJECT")
      private String subject;
  
      @Column(name = "MAX_MARKS")
      private String maxMarks;
  
      @Column(name = "MARKS_OBTAINED")
      private String marksObtained;
  
      @Column(name = "RESULT")
      private String result;
  
      @ManyToMany(fetch = FetchType.LAZY, 
                  mappedBy = "test", 
                  cascade= {CascadeType.PERSIST})
  
      private Set&lt;Student&gt; student = new HashSet&lt;Student&gt;();
  }
  </code></pre>
  
  <p>Student:</p>
  
  <pre><code>@Entity
  @Table(name = "STUDENT")
  public class Student 
  {
      @Id
      @Column(name = "STUDENT_ID")
      @GeneratedValue
      private Long studentId;
  
      @Column(name = "FIRSTNAME")
      private String firstname;
  
      @Column(name = "LASTNAME")
      private String lastname;
  
      @Column(name = "EMAIL")
      private String email;
  
      @Column(name = "PHONE")
      private String phone;
  
      @ManyToMany(cascade = { CascadeType.ALL })
      @JoinTable(name = "STUDENT_TEST", 
      joinColumns = { @JoinColumn(name = "STUDENT_ID", updatable = false) }, 
      inverseJoinColumns = { @JoinColumn(name = "TEST_ID", updatable = false) })
      private Set&lt;Test&gt; test = new HashSet&lt;Test&gt;();
   }
  </code></pre>
  
  <p>I have created test record separately.
  Now I wanted to insert a Student with relationship record.</p>
  
  <p>The following code is trying to 
  update student record
  update test record
  insert a record in relationship table (student_test).</p>
  
  <pre><code>Test test1 = new Test();
  test1.setTestId(Long.valueOf("3"));
  
  student1.getTest().add(test1);
  student1.setStudentId(Long.valueOf("1"));
  
  try  
  {
      session.saveOrUpdate(student1);
  } 
  
  catch (HibernateException e)  
  {
      e.printStackTrace();
  }
  
  session.getTransaction().commit();
  session.close();`
  </code></pre>
  
  <p>My question is, I need to insert a record into Student and relationship table(student_test) only and it should not update record in test table. </p>