Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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>

like image 468
harsh Avatar asked Sep 25 '13 11:09

harsh


People also ask

How index is defined in JPA?

JPA allows us to achieve that by defining indexes from our code using @Index. This annotation is interpreted by the schema generation process, creating artifacts automatically. Note that it's not necessary to specify any index for our entities.

What is indexing in Hibernate?

Indexing. The short answer is that indexing is automatic: Hibernate Search will transparently index every entity each time it's persisted, updated or removed through Hibernate ORM. Its mission is to keep the index and your database in sync, allowing you to forget about this problem.

What is the use of @column annotation?

@Column annotation is used for Adding the column the name in the table of a particular MySQL database.

What is @entity annotation in spring boot?

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.


1 Answers

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 DBIssues belonging to a particular DBProject frequently, so it would make sense to index that column in the table holding DBIssue.

like image 197
chrylis -cautiouslyoptimistic- Avatar answered Nov 14 '22 21:11

chrylis -cautiouslyoptimistic-