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