title: Spring-Hibernate, Many to Many relationship query
body: <p>I have two bean classes Student and Course which has many to many relationship with each other. For eg. one student can register for multiple courses and vice versa. I have used HibernateTemplate to save objects into Oracle DB. Following are Student, Course and StudentDao classes.</p>
  
  <p><strong>Student Class</strong></p>
  
  <pre><code>package com.springhibernate;
  
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.ManyToMany;
  import javax.persistence.Table;
  
  @Entity
  @Table(name="Student")
  
  public class Student {
      private int studentId;
      private String firstName;
      private String lastName;
      private Set&lt;Course&gt; courses;
  
  @Id
  @Column(name="student_id")
  public int getStudentId() {
      return studentId;
  }
  public void setStudentId(int studentId) {
      this.studentId = studentId;
  }
  
  @Column(name="first_name")
  public String getFirstName() {
      return firstName;
  }
  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }
  
  @Column(name="last_name")
  public String getLastName() {
      return lastName;
  }
  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
  
  @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 courses;
  }
  public void setCourses(Set&lt;Course&gt; courses) {
      this.courses = courses;
  }
  
  }
  </code></pre>
  
  <p><strong>StudentDao class</strong></p>
  
  <pre><code>package com.springhibernate;
  
  import java.util.List;
  
  import org.springframework.orm.hibernate3.HibernateTemplate;
  
  public class StudentDao {
  
  private static Helper helper = new Helper();
  
  HibernateTemplate template;
  public void setTemplate(HibernateTemplate template) {
      this.template = template;
  }
  
  // method to save student
  public void saveStudent(Student s) {
      template.save(s);
  }
  
  // method to return one employee of given id
  public Student getById(int id) {
      Student s = (Student) template.get(Student.class, id);
      return s;
  }
  
  public List&lt;Course&gt; findCourse(){
      List&lt;Course&gt; list = template.find("from Course");
      return list;
  }
  }
  </code></pre>
  
  <p><strong>Course Class</strong></p>
  
  <pre><code>package com.springhibernate;
  
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.ManyToMany;
  import javax.persistence.Table;
  
  
  @Entity
  @Table(name="Course")
  public class Course {
  
  private int courseId;
  private String courseName;
  private Set&lt;Student&gt; students;
  
  @Id
  @Column(name="course_id")
  public int getCourseId() {
      return courseId;
  }
  public void setCourseId(int courseId) {
      this.courseId = courseId;
  }
  
  @Column(name="course_name")
  public String getCourseName() {
      return courseName;
  }
  public void setCourseName(String courseName) {
      this.courseName = courseName;
  }
  
  @ManyToMany(cascade=CascadeType.ALL,mappedBy="courses")
  public Set&lt;Student&gt; getStudents() {
      return students;
  }
  public void setStudents(Set&lt;Student&gt; students) {
      this.students = students;
  }
  
  @Override
  public int hashCode() {
      // TODO Auto-generated method stub
      return this.courseId;
  }
  
  @Override
  public boolean equals(Object o) {
      // TODO Auto-generated method stub
      Course temp = (Course) o;
      return (this.courseId==temp.courseId);
  
  }
  }
  </code></pre>
  
  <p>I have following two queries</p>
  
  <ol>
  <li><p>I am able to save data in student_course table successfully. I was wondering if I want to retrieve data from student_course table, how can I do it using HibernateTemplate or is there any other way to do so?
  For example, query is like</p>
  
  <pre><code>select course_id from student_course where student_id=1
  </code></pre>
  
  <p>Please note I want just the course id column not complete row.</p></li>
  <li><p>If in student_course table I want one more column say course_name (from course table), how can I do that?</p></li>
  </ol>