title: How to use index column in many-to-many association in criteria in Hibernate?
body: <p>I'm using Hibernate 3 with Java.</p>
  
  <p>My POJOs classes are as follows:</p>
  
  <p><strong>Teacher.java</strong></p>
  
  <pre><code>private long id;
  private String teacherName;
  private List&lt;Student&gt; students;
  // getter-setter of all
  </code></pre>
  
  <p><strong>Student.java</strong></p>
  
  <pre><code> private long id;
   private String studentName;
  
  // getter-setter of both
  </code></pre>
  
  <p><strong>Teacher.hbm.xml</strong></p>
  
  <pre><code>&lt;class name="Teacher" table="teacher_master"&gt;
      &lt;id name="id" column="id" type="long"&gt;
          &lt;generator class="native"&gt;&lt;/generator&gt;
      &lt;/id&gt;
  
      &lt;property column="teacher_name" name="teacherName" type="string" /&gt;
  
      &lt;list name="students" cascade="refresh"&gt;
          &lt;key column="teacher_id"/&gt;
          &lt;index column="student_position" type="integer"/&gt;
          &lt;one-to-many class="Student"/&gt;
      &lt;/list&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p><strong>Student.hbm.xml</strong> contains mappings for <strong>id</strong> &amp; <strong>studentName</strong> properties.</p>
  
  <p>My database structure looks like the following:</p>
  
  <p><strong>teacher_master</strong></p>
  
  <pre><code>id  | teacher_name
  ----|--------------
  1   | teacher1
  2   | teacher2
  </code></pre>
  
  <p><strong>student_master</strong></p>
  
  <pre><code>id  | student_name  | teacher_id  | student_position
  ----|---------------|-------------|------------------
  1   | student1      |      1      |      0
  2   | student2      |      1      |      2
  3   | student3      |      1      |      1
  </code></pre>
  
  <p>Now I want to fetch all Students attached to Teacher with id = 1, <strong>in the order of student_position</strong>.</p>
  
  <p>I've written the following criteria:</p>
  
  <pre><code>List&lt;Long&gt; ids = new ArrayList&lt;Long&gt;();
  ids.add(1l);
  ids.add(2l);
  ids.add(3l);
  Criteria criteria = session.createCriteria(Student.class);
  criteria.add(Restrictions.in("id", ids));
  List&lt;Student&gt; students = criteria.list();
  </code></pre>
  
  <p>Here <strong>students</strong> will give me the records in order of the primary key that is 1,2,3.</p>
  
  <p>I want these records to be in order of their <strong>student_position</strong> that is 1,3,2.</p>
  
  <p>How can I achieve this?</p>