@Index 注释导致 "This annotation is not applicable to target 'member property with backing field'"

@Index annotation leads to "This annotation is not applicable to target 'member property with backing field'"

我正在尝试使用 @Index 注释在外键上创建索引。不幸的是,编译器抱怨以下消息:

This annotation is not applicable to target 'member property with backing field'

我做错了什么?

@Entity
@Table(name = "my_entity")
class MyEntity(someValue: Long) : BaseEntity(someValue) {

    // .. some fields
    
    @OneToOne
    @JoinColumn(name = "another_entity")
    @Index(name = "ix_another_entity")
    var anotherEntity: AnotherEntity? = null
}

@Index 不能像这样使用(无论是在 java 还是在 kotlin 中),您可以使用它,例如作为 @Table 注释的一部分:

@Table(name= "my_entity", indexes = [ Index(columnList = "another_entity") ])

Specifying an Index (Non-Unique Key) Using JPA

(请注意,例如 MySQL 自动为外键创建索引:Does MySQL index foreign key columns automatically?