JPA: defining an index column [duplicate]

青春壹個敷衍的年華 提交于 2019-12-18 02:49:07

问题


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;

  ...

回答1:


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"}),
   }
)



回答2:


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

Details are here.




回答3:


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

For EclipseLink see the @Index page.



来源:https://stackoverflow.com/questions/6453231/jpa-defining-an-index-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!