Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA: defining an index column [duplicate]

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;

  ...
like image 249
lili Avatar asked Jun 23 '11 11:06

lili


2 Answers

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"}),
   }
)
like image 180
Augusto Avatar answered Oct 24 '22 16:10

Augusto


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

Details are here.

like image 29
expert Avatar answered Oct 24 '22 15:10

expert