Java 休眠:何时使用 @Index 注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19003334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:16:56  来源:igfitidea点击:

Hibernate: When to use @Index annotation

javasqloraclehibernate

提问by harsh

In my scenario, I have a schema generation script to create tables and required indexes. I am wondering is there any need to define @Indexannotation in hibernate entities as well, if so why?

在我的场景中,我有一个模式生成脚本来创建表和所需的索引。我想知道是否还需要@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:

休眠配置:

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

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

采纳答案by chrylis -cautiouslyoptimistic-

I presume you're asking about the Hibernate @Indexannotation, which has essentially been imported into JPA 2.1. You would use @Indexanywhere 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. In this case, for example, you are probably going to want to "select the DBIssues belonging to a particular DBProjectfrequently, so it would make sense to index that column in the table holding DBIssue.

我想您是在询问 Hibernate@Index注释,它基本上已导入 JPA 2.1。您可以@Index在任何您会主动告诉关系数据库索引列的地方使用,主要是在您知道将进行大量查找的字段上。例如,在这种情况下,您可能希望“经常选择DBIssue属于特定对象的s DBProject,因此在包含DBIssue.