Jakarta Persistence 4.0 - Index DDL のカスタマイズ -

Jakarta Persistence 4.0 では、Index アノテーションに(3.2 で追加された options に加え)、kindtype が追加された。

プルリクエストは以下

Add a way to customize SQL DDL for Index

Issue は以下


以下のIndexアノテーションに

public @interface Index {
    String name() default "";
    String columnList();
    boolean unique() default false;
    String options() default "";
}

kind と type が追加された。

public @interface Index {

    // ...

    /**
     * (Optional) A SQL fragment representing the index kind,
     * usually inserted before the keyword {@code index} in 
     * the generated DDL which creates this index.
     *
     * @since 4.0
     */
    String kind() default "";

    /**
     * (Optional) A SQL fragment representing the index type.
     * When specified, it is inserted into the generated DDL 
     * which creates this index.
     *
     * @since 4.0
     */
    String type() default "";

}

kind は、MariaDB で言う [UNIQUE|FULLTEXT|SPATIAL|VECTOR]、type は BTREE など。以下の大文字の箇所となる

create UNIQUE index index_name on table_name using BTREE (column_name, ...);


以下のように指定できる。

@Entity
@Table(name = "employees", indexes = {
    @Index(name = "idx_emp_email", columnList = "email", kind = "unique", type = "rtree"),
    @Index(name = "idx_emp_names", columnList = "lastName, firstName")
})
public class Employee { ... }

スキーマ生成でインデックスの生成を指定する場合の選択肢が広がった。