title: Specifying an Index (Non-Unique Key) Using JPA
body: <p>How do you define a field, eg <code>email</code> as having an index using JPA annotations. We need a non-unique key on <code>email</code> because there are literally millions of queries on this field per day, and its a bit slow without the key.</p>
  
  <pre><code>@Entity
  @Table(name="person", 
         uniqueConstraints=@UniqueConstraint(columnNames={"code", "uid"}))
  public class Person {
      // Unique on code and uid
      public String code;
      public String uid;
  
      public String username;
      public String name;
      public String email;
  }
  </code></pre>
  
  <p>I have seen a hibernate specific annotation but I am trying to avoid vendor specific solutions as we are still deciding between hibernate and datanucleus.</p>
  
  <p>UPDATE:</p>
  
  <p>As of JPA 2.1, you can do this. See: <a href="https://stackoverflow.com/questions/17620405/the-annotation-index-is-disallowed-for-this-location">The annotation @Index is disallowed for this location</a></p>