问题
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