此位置不允许使用注释@Index

当试图从javax.persistence使用@Index注释时,Eclipse给了我这个错误。

我在java.util.Date字段之前使用它,在使用@Entity注释的类中。

之前,我在完全相同的地方使用org.hibernate.annotations.Index并且它很好。

在我将hibernate-core4.1.9.Final升级到4.3.0.Beta3并将hibernate-commons-annotation4.0.1升级到4.0.2之后,问题就出现了 。 它说@Index已被弃用并推荐使用javax.persistence

我发现的所有文档和示例都将@Index放在了类成员之前。 我错过了什么?

JPA索引注释只能用作@ @Table ,@ @SecondaryTable等其他注释的一部分(请参阅javadoc中的另请参见部分):

 @Table(indexes = { @Index(...) }) 

请参见: https : //docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#annotations-jpa-index

用这个:

 @Table .......,indexes = @Index(columnList = ("COLUMN_NAME"),name="CUSTOM NAME AT INDEX",unique=false) ...... 

如果您使用Eclipselink您可以将此导入添加到您的class级:

 import org.eclipse.persistence.annotations.Index; 

然后将@Index添加到您的字段,如下所示:

 public class FooClass { @Index int field1; } 

要么

 @Index(columnNames = {"field1", "field2"}) public class FooClass { int field1; int field2; }