Loading...

How to Define an Index with Spring Data JPA

JPA
June 28, 2023
3 minutes to read
Share this post:

In this article, we will deal with the definition of indexes in Spring Data JPA and the Hibernate Framework. Our focus is on defining an index for a non-primary key and using the @Index annotation.

The proper use of indexes plays a crucial role in optimizing the performance of your database, as they enable faster data queries and overall improve database performance.

Defining an Index with the @Index Annotation

With JPA and Hibernate, you can define indexes on entities and non-entity tables such as @SecondaryTable, @CollectionTable, and @JoinTable using the @Index annotation. This serves to optimize database performance and customization.

Suppose you have an entity Person and want to create a unique index for the column email. Your entity class could look like this:

@Entity
@Table(name = "persons",
       indexes = {@Index(name = "idx_email",  columnList="email", unique = true)})
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;
}

In the @Table annotation, we have defined an index named idx_email based on the email column. The unique attribute is set to true, meaning that we have created a Unique Index.

Unique Attribute: @Index vs @Column

It’s important to understand that the unique attribute in the @Index and @Column annotation has different meanings. In the @Column annotation, unique = true means that the column must contain a unique value, similar to a primary key. On the other hand, unique = true in the @Index annotation creates a unique index for the column.

Defining an Index on Multiple Columns

With JPA and Hibernate, you can create an index on multiple columns by using the columnList attribute. The order of the columns in columnList defines the order of the index. Here is an example:

@Entity
@Table(name = "persons",
       indexes = {@Index(name = "idx_name_email", columnList="name, email")})
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;
}

In this example, an index is created on the name and email columns. The name of the index is idx_name_email.

Similar to the @Table and @Column annotation, @Index is a configuration for creating the DDL (Data Definition Language). This is only automated if Hibernate is configured to create tables. In a live application, you should use a tool like flyway or liquibase and only allow Hibernate to validate the schema.

Defining indexes with Spring Data JPA and Hibernate is a powerful tool for improving the performance of your application. By using the @Index annotation and understanding unique indexes as well as the role of primary and non-primary keys, you can efficiently optimize and customize your database.

Have you heard of Marcus' Backend Newsletter?

New ideas. Every week!
Top