title: hibernate many-to-many mapping with orderby clause does not work
body: <p>I have a question regarding the hibernate many-to-many mappings with the use of the @OrderBy annotation. Let's say we have table A, B and the glue table A_B. I want the collection A.bs be sorted by field B.date. When I run the query I get the exception saying:</p>
  
  <pre><code>org.hibernate.exception.SQLGrammarException: ERROR: column b1_.date does not exist
  </code></pre>
  
  <p>The problem is that hibernate creates query that is searching for the column in the A_B table and not in the B table. I have been searching through the web and encountered the same question without a proper answer (<a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=950032&amp;sid=b7e3e9ff23a6da3b5fff2f5336d3bb1b" rel="nofollow noreferrer">https://forum.hibernate.org/viewtopic.php?f=1&amp;t=950032&amp;sid=b7e3e9ff23a6da3b5fff2f5336d3bb1b</a>) but at the same time issues dealing with similar problems and the answers were saying that such configuration is without a problem (<a href="https://stackoverflow.com/questions/14294712/hibernate-sorting-manytomany-mapping#">Hibernate : Sorting ManyToMany mapping</a>). In the @OrderBy clause I tried using the field name in the domain object, column name in the DB but to no avail. Any progress in this issue? Any help or hints appreciated. Thx. :)</p>
  
  <p>These are details:</p>
  
  <pre><code>@Entity
  @Table(name="A")
  public class A implements java.io.Serializable {
  
      private Set&lt;B&gt; bs = new HashSet&lt;B&gt;(0);
  
      @ManyToMany(fetch=FetchType.LAZY)
      @JoinTable(name="A_B", joinColumns = { 
          @JoinColumn(name="a_id", nullable=false, updatable=false) }, inverseJoinColumns = { 
          @JoinColumn(name="b_id", nullable=false, updatable=false) })
      @OrderBy(clause = "date desc")
      public Set&lt;B&gt; getBs() {
          return this.bs;
      }
  }
  
  @Entity
  @Table(name="B")
  public class B implements java.io.Serializable {
  
      private Date date;
  
      /* setters and getters */
  }
  </code></pre>
  
  <p>I omitted ids and other details. If I delete the @OrderBy annotation everything works like a charm.</p>
  
  <p>More details about my getter on date:</p>
  
  <pre><code>@Temporal(TemporalType.TIMESTAMP)
  @Column(name="date", nullable=false, length=29)
  public Date getDate() {
      return this.date;
  }
  </code></pre>