title: How to write Order by expression in JPQL
body: <p>PostgreSQL and MySQL offers to write expression into <code>ORDER BY</code> clause in SQL query. It allows to sort items by some column but the special values are on the top. The SQL looks like this one. ( works in Postgres )</p>
  
  <pre><code>select * from article order by id = 4, id desc;
  </code></pre>
  
  <p>Now I want to write it in the JPQL but it doesn't work. My attempt is:</p>
  
  <pre><code>@NamedQuery(name = "Article.special", query = "SELECT a FROM Article a ORDER BY ( a.id = :id ) DESC, a.id DESC")
  </code></pre>
  
  <p>This is JPA 1.0 with Hibernate driver. Application server throws this exception on deploy.</p>
  
  <pre><code>ERROR [SessionFactoryImpl] Error in named query: Article.special
  org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: = near line 1, column 73 [SELECT a FROM cz.cvut.fel.sk.model.department.Article a ORDER BY ( a.id = :id ) DESC, a.id DESC]
   at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
  </code></pre>
  
  <p>Thanks a lot.</p>