【问题标题】:The annotation @Index is disallowed for this location此位置不允许使用注释 @Index
【发布时间】:2013-07-11 07:41:13
【问题描述】:

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

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

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

问题是在我将 hibernate-core4.1.9.Final 升级到 4.3.0.Beta3 之后开始的>hibernate-commons-annotations 从 4.0.14.0.2。它说@Index 已弃用并推荐javax.persistence

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

【问题讨论】:

    标签: java jpa hibernate-annotations


    【解决方案1】:

    JPA 索引注解只能用作另一个注解的一部分,例如@Table@SecondaryTable 等(参见javadoc 中的另请参阅部分):

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

    【讨论】:

      【解决方案2】:

      请看这里: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)
      ......
      

      【讨论】:

        【解决方案3】:

        如果您使用Eclipselink,您可以将此导入添加到您的课程中:

        import org.eclipse.persistence.annotations.Index;
        

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

        public class FooClass {
           @Index
           int field1;
        }
        

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

        【讨论】:

        • 我认为问题是关于 JPA 2.1 @Index 注释,而不是专有的。
        猜你喜欢
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多