2013-06-24 3 views
6

Tôi đang cố gắng biên dịch một ứng dụng web Java mà tôi đang viết và tôi nhận được các lỗi biên dịch mà tôi không biết phải làm gì. googling tôi đã làm, tôi thấy this SO câu hỏi, nhưng hỏi đã được sử dụng EJB, trong khi lỗi của tôi là trong một lớp thực thể JPA.lỗi: loại chú thích không áp dụng cho loại khai báo này

đây là lỗi maven xây dựng.

[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 

Và đây là tài khoản của tôi tệp lớp học

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; 
    } 

} 

Tôi đã xem JavaDoc javax.persistence.Index, và tôi đã khai báo đúng, vì vậy tôi bị mắc kẹt ở đây, Có ai đó biết tôi đang làm gì sai không? Cảm ơn bạn đã dành thời gian và sự quan tâm của bạn.

+0

là hợp lý như định nghĩa của bạn (và sự kiên trì của JDO) spec hỗ trợ theo cách đó, trong số các cách khác để chỉ định chỉ mục), bạn chỉ có thể chỉ định các chỉ mục trong JPA qua chú thích XXXTable, xem trả lời của người dùng2507946) – DataNucleus

Trả lời

2

Từ JavaDoc tại đây: http://docs.oracle.com/javaee/7/api/javax/persistence/Index.html, có vẻ như chú thích chỉ mục có @Target(value={}) có nghĩa là nó nên được sử dụng như một phần của chú thích phức tạp và không thể sử dụng trực tiếp.

Trích dẫn từ https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation, có vẻ như nó chỉ có thể được sử dụng như một phần của chú thích JPA: "@Index - Chỉ mục cho khóa chính được tạo theo mặc định trong cơ sở dữ liệu. Chú thích mới này sẽ cho phép xác định chỉ mục bổ sung , trên một hoặc nhiều cột, để có hiệu suất tốt hơn. Điều này được chỉ định như một phần của @Table, @SecondaryTable, @CollectionTable, @JoinTable và @TableGenerator "