简体   繁体   English

Hibernate:何时使用@Index注释

[英]Hibernate: When to use @Index annotation

In my scenario, I have a schema generation script to create tables and required indexes. 在我的场景中,我有一个模式生成脚本来创建表和所需的索引。 I am wondering is there any need to define @Index annotation in hibernate entities as well, if so why? 我想知道是否有必要在hibernate实体中定义@Index注释,如果是这样,为什么呢?

Script: 脚本:

create table issues (id, project_id, .., status_id)

create index idx_issues_projid on issues (project_id)

Entity: 实体:

@Table(name="issues")
public class DBIssue {

..
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "PROJECT_ID")
  @Index(name="INDEX_TFW_ISSUE_PROJECT_ID")
  private DBProject project;
}

Hibernate configuration: Hibernate配置:

<property name="hibernate.hbm2ddl.auto">off</property>

I presume you're asking about the Hibernate @Index annotation, which has essentially been imported into JPA 2.1. 我假设你问的是Hibernate @Index注释,它基本上被导入到JPA 2.1中。 You would use @Index anywhere you would otherwise proactively tell a relational database to index a column, primarily on a field where you know you'll be doing lots of lookups. 您可以在任何地方使用@Index ,否则您会主动告诉关系数据库索引列,主要是在您知道要进行大量查找的字段上。 In this case, for example, you are probably going to want to "select the DBIssue s belonging to a particular DBProject frequently, so it would make sense to index that column in the table holding DBIssue . 在这种情况下,例如,你可能会想“选择DBIssue小号属于特定DBProject频繁,所以这将是有意义的索引表中保存该列DBIssue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM