10

Possible Duplicate:
Specifying an index (non unique key) using JPA

Is there a way to define index on enitity column, to improve searches performance? I saw that hibernate gives @Index and @IndexColumn, but I am looking for JPA way to do it.

thanks

Here is an example of my entity, I need to index a name column

@Entity
@Table(name = "MY_TABLE")
public class MyEntity {
    private long id;
    private String name;
    private String sourceInfo;

  ...
Community
  • 1
  • 1
lili
  • 1,866
  • 8
  • 29
  • 50

3 Answers3

30

No, jpa doesn't provide any feature to define or create indexes. For some (unknown to me) reason, it's only possible to create unique indexes in JPA.

If you are not using hibernate or if you really don't want to use those annotations, you'll need to build your annotations and processor to output or update the database accordingly, but I don't think it's very trivial (and it's definitely, non standard)

Edit

Here's an example of how to define an index with Hibernate

@org.hibernate.annotations.Table(
   appliesTo = "table_name",
   indexes = {
      @Index(name="single_column_index", columnNames = "col"),
      @Index(name="multi_column_index", columnNames = {"col1", "col2"}),
   }
)
Community
  • 1
  • 1
Augusto
  • 28,839
  • 5
  • 58
  • 88
  • 1
    FWIW JPA also doesn't allow the user to define Foreign-Key details; incomplete ORM specification. Only JDO allows complete control over RDBMS constraints – DataNucleus Jun 23 '11 at 12:35
  • Thank you, I use hibernate but wanted to avoid vendor-specific dependency. Do you have an example of hibernate solution for this problem? – lili Jun 23 '11 at 14:43
  • @lili, please check the edit with the example. @DataNucleus: you are so, so right, JPA is almost a joke with its basic features. – Augusto Jun 23 '11 at 16:19
  • 5
    For what it's worth, JPA 2.1 can do indexes in the @Table annotation now, see: https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation – Joel Pearson May 18 '14 at 04:08
  • See: http://stackoverflow.com/a/22658951/16206 – borjab Dec 05 '14 at 11:34
2

There is no standard JPA annotation. (JPA does not even require DDL generation).

For EclipseLink see the @Index page.

stites
  • 4,903
  • 5
  • 32
  • 43
James
  • 17,965
  • 11
  • 91
  • 146
2

OpenJPA allows you to specify non-standard annotation to define index on property.

Details are here.

stites
  • 4,903
  • 5
  • 32
  • 43
expert
  • 29,290
  • 30
  • 110
  • 214