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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17620405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:29:26  来源:igfitidea点击:

The annotation @Index is disallowed for this location

javajpahibernate-annotations

提问by Vituel

When trying to use the @Indexannotation from javax.persistence, Eclipse gives me this error.

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

I'm using it right before a java.util.Datefield, inside a class annotated with @Entity.

我在一个java.util.Date字段之前使用它,在一个用@Entity.

Before, I was using org.hibernate.annotations.Indexin the exact same place and it was fine.

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

The problem started after I've upgraded hibernate-corefrom 4.1.9.Finalto 4.3.0.Beta3and hibernate-commons-annotations from 4.0.1to 4.0.2. It says @Indexis deprecated and recommends the javax.persistenceone.

在我将hibernate-core4.1.9.Final升级到4.3.0.Beta3并将hibernate-commons-annotation4.0.1升级到4.0.2之后,问题就开始了。它说不@Index推荐使用并推荐javax.persistence一个。

All docs and examples I've found put @Indexbefore class members. What am I missing?

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

回答by JB Nizet

The JPA Index annotation can only be used as part of another annotation like @Table, @SecondaryTable, etc. (see the See Also section in the javadoc):

在JPA指数注释只能被用作像另一个注释的一部分@Table@SecondaryTable等(参见在所述另请参见部的Javadoc):

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

回答by Vituel

See here: https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#annotations-jpa-index

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

use this:

用这个:

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

回答by Joe Almore

If you use Eclipselinkyou can add this import to your class:

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

import org.eclipse.persistence.annotations.Index;

Then add your @Indexto your field like this:

然后@Index像这样将您的添加到您的字段中:

public class FooClass {
   @Index
   int field1;
}

or

或者

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