java 错误:注解类型不适用于此类声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17267583/
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
error: annotation type not applicable to this kind of declaration
提问by Levi Campbell
I'm trying to compile a Java web app that I'm writing, and I"m getting compile errors that I'm not sure what to do with. From the googling I've done, I found thisSO question, but the asker is using EJB, while my error is in a JPA entity class.
我正在尝试编译我正在编写的 Java Web 应用程序,但我遇到了编译错误,我不确定该怎么办。从我所做的谷歌搜索中,我发现了这个问题,但是提问者正在使用 EJB,而我的错误是在 JPA 实体类中。
Here's the maven build error.
这是 Maven 构建错误。
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.988s
[INFO] Finished at: Mon Jun 24 02:39:51 UTC 2013
[INFO] Final Memory: 15M/247M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project donebox: Compilation failure: Compilation failure:
[ERROR] /home/cl-server/builder/tomcat-ide-builder/temp/build-1236514164814552082/src/main/java/net/donebox/accounts/User.java:[50,4] error: annotation type not applicable to this kind of declaration
[ERROR] /home/cl-server/builder/tomcat-ide-builder/temp/build-1236514164814552082/src/main/java/net/donebox/accounts/User.java:[60,4] error: annotation type not applicable to this kind of declaration
[ERROR] /home/cl-server/builder/tomcat-ide-builder/temp/build-1236514164814552082/src/main/java/net/donebox/accounts/Role.java:[53,4] error: annotation type not applicable to this kind of declaration
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
And here's my User class file.
这是我的用户类文件。
package net.donebox.accounts;
import java.io.Serializable;
import java.util.UUID;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Index;
import javax.persistence.ManyToMany;
import javax.persistence.JoinTable;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
@Entity
@Table(name="users")
public class User {
private UUID id;
private String username;
private String email;
private String password;
private Set<Role> roles = new HashSet<Role>();
@Id
@GeneratedValue
public UUID getId() {
return id;
}
public void setId() {
this.id = UUID.randomUUID();
}
/**
* Returns the username associated with this user account;
*
* @return the username associated with this user account;
*/
@Basic
@Column(length=100)
@Index(name="idx_users_username", columnList="username") //Error here.
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Index(name="idx_users_email", columnList="email") // And here.
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
/**
* Returns the password for this user.
*
* @return this user's password
*/
@Basic(optional=false)
@Column(length=255)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@ManyToMany
@JoinTable(name="users_roles")
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
I looked at the javax.persistence.Index JavaDoc, and I have the declaration correct, so I'm stumped here, Does someone know what I'm doing wrong? Thank you for your time and consideration.
我查看了 javax.persistence.Index JavaDoc,我的声明是正确的,所以我在这里被难住了,有人知道我做错了什么吗?感谢您的时间和考虑。
采纳答案by user2507946
From the JavaDoc here: http://docs.oracle.com/javaee/7/api/javax/persistence/Index.html, it seems that Index annotation has @Target(value={})
which means it should be used as a part of complex annotation and can't be used directly.
从这里的JavaDoc:http: //docs.oracle.com/javaee/7/api/javax/persistence/Index.html,似乎索引注释具有@Target(value={})
这意味着它应该用作复杂注释的一部分并且可以'不能直接使用。
Quoting from https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation, it seems that it can be used only as part of JPA annotations: "@Index - An index for the primary key is generated by default in a database. This new annotation will allow to define additional indexes, over a single or multiple columns, for a better performance. This is specified as part of @Table, @SecondaryTable, @CollectionTable, @JoinTable, and @TableGenerator"
引自https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation,它似乎只能用作 JPA 注释的一部分:“ @Index - 默认情况下在数据库中生成主键的索引。这个新的注解将允许在单个或多个列上定义额外的索引,以获得更好的性能。这被指定为@Table、@SecondaryTable、@CollectionTable、@JoinTable 和 @TableGenerator 的一部分“