title: JPQL Order by Enum
body: <p>I'd like to fetch data from a table ordered by priority column that is enum. We use javaee 6, GF 3.1.</p>
  
  <p>Our entity looks like this</p>
  
  <pre><code>@Entity
  @Table(name="ITEMS")
  @NamedQueries({
      @NamedQuery(
          name = "Item.findbyPriority",
          query = "select i from Item i where ORDER BY i.priority DESC" )
  })
  public class Item implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Integer id;
  
  
  
      String data inputFile;
  
  
      @Enumerated(EnumType.ORDINAL)
      Priority priority;
  
  
  ...
  }
  </code></pre>
  
  <p>Priority is defined</p>
  
  <pre><code>public enum Priority {
  
      HIGH,
      NORMAL;
  }
  </code></pre>
  
  <p>However <strong>Item.findbyPriority</strong> query sorts by ordinal, and returns result in wrong order. If I change Priority class to </p>
  
  <pre><code>public enum Priority {
  
          NORMAL,
          HIGH;
      }
  </code></pre>
  
  <p>It works OK. But that is not really robust. What if I add new Priority use or reorder it?</p>
  
  <p>I tried to override compareTo method but it is final in Enum class.</p>
  
  <p>What can I do in this case? How can it be done in nice way?</p>
  
  <p>I know I could store priority as number etc, but than I have to sync entities to Enum by hand, which is not super nice.
  Maybe @PrePersist, @PreUpldate and @PostLoad methods are the way to go? How would then jpql query look like?</p>
  
  <p>thanks,</p>
  
  <p>milan</p>