I am trying to create an index on a foreign key using the @Index
annotation. Unfortunately, the compiler complains with the following message:
This annotation is not applicable to target 'member property with backing field'
What am I doing wrong here?
@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
}
CodePudding user response:
The @Index
cannot be used like this (neither in java nor in kotlin), instead you can use it e.g. as part of the @Table
annotation:
@Table(name= "my_entity", indexes = [ Index(columnList = "another_entity") ])
Specifying an Index (Non-Unique Key) Using JPA
(note that e.g. MySQL auto creates an index for foreign keys: Does MySQL index foreign key columns automatically? )