The annotation @Index is disallowed for this location

痞子三分冷 提交于 2019-11-27 03:04:09

问题


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

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

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

The problem started after I've upgraded hibernate-core from 4.1.9.Final to 4.3.0.Beta3 and hibernate-commons-annotations from 4.0.1 to 4.0.2. It says @Index is deprecated and recommends the javax.persistence one.

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


回答1:


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):

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



回答2:


See here: 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)
......



回答3:


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

import org.eclipse.persistence.annotations.Index;

Then add your @Index to your field like this:

public class FooClass {
   @Index
   int field1;
}

or

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


来源:https://stackoverflow.com/questions/17620405/the-annotation-index-is-disallowed-for-this-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!