title: How to join intermediate table in ManyToMany mappings in the hibernate
body: <p>Is there a way I can join intermediate table in the <code>ManyToMany</code> table. You can consider the example given at <a href="https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html" rel="nofollow noreferrer">https://dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-using-annotations-1.html</a></p>
  
  <p><a href="https://i.stack.imgur.com/lZkw8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lZkw8.png" alt="enter image description here"></a></p>
  
  <p>As an example in the above <code>Student</code> to <code>Course</code> many to many relation, we can have tables <code>STUDENT</code>, <code>COURSE</code> and <code>STUDENT_COURSE</code>.</p>
  
  <p>My question is to check if there is any way we can provide this in hibernate in java so that <code>Student</code> class has mapping for <code>STUDENT_COURSE</code> intermediate table instead of <code>COURSE</code> table?</p>
  
  <p>I want to modify below class to get the above output:-</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Table(name = "STUDENT")
  public class Student {
  
      private long studentId;
      private String studentName;
      private Set&lt;Course&gt; courses = new HashSet&lt;Course&gt;(0);
  
      public Student() {
      }
  
      public Student(String studentName) {
          this.studentName = studentName;
      }
  
      public Student(String studentName, Set&lt;Course&gt; courses) {
          this.studentName = studentName;
          this.courses = courses;
      }
  
      @Id
      @GeneratedValue
      @Column(name = "STUDENT_ID")
      public long getStudentId() {
          return this.studentId;
      }
  
      public void setStudentId(long studentId) {
          this.studentId = studentId;
      }
  
      @Column(name = "STUDENT_NAME", nullable = false, length = 100)
      public String getStudentName() {
          return this.studentName;
      }
  
      public void setStudentName(String studentName) {
          this.studentName = studentName;
      }
  
      @ManyToMany(cascade = CascadeType.ALL)
      @JoinTable(name = "STUDENT_COURSE",
                 joinColumns = { @JoinColumn(name = "STUDENT_ID") },
                 inverseJoinColumns = { @JoinColumn(name = "COURSE_ID") })
      public Set&lt;Course&gt; getCourses() {
          return this.courses;
      }
  
      public void setCourses(Set&lt;Course&gt; courses) {
          this.courses = courses;
      }
  }
  </code></pre>