DATAAnnotations - 重复注释

时间:2014-04-12 22:55:18

标签: java mysql sql database annotations

作为课程项目的一部分,我们被要求注释我们的代码。

以下代码

import java.lang.annotations.*;
@Target({ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DBAnnotation {
 String variable () default "";
 String table () default "";
 String column () default "";
 boolean isSource () default false; 
}


public static void addFileToDB(String fileName, String fileLocation, int offerID){
    @DBAnnotation (variable = "fileName",  table = "files", column = "FileName", isSource = true)
    @DBAnnotation (variable = "fileLocation",  table = "files", column = "fileLocation", isSource = true)




    String SQLFileSelect = "SELECT FileName FROM files WHERE OfferID = ? AND FileLocation = ?;";
.
.
.
}

我收到以下错误。

Duplicate annotation @File.DBAnnotation. Repeated annotations are allowed only at source level 1.8 or above

但如果我把它改为......

public @interface DBAnnotation {
     String[] variable () default "";
     String table () default "";
     String[] column () default "";
     boolean[] isSource () default false; 
    }
.
.
.
@DBAnnotation (
            variable = {"fileName","fileLocation"},  
            table = "files", 
            column = {"FileName","fileLocation"}, 
            isSource = true)

然后它不会给出任何错误。

我关注的是,对于变量fileLocation,将DBAnnotation视为 variable =“fileLocation”,table =“files”,column =“fileLocation”,isSource = true

或将被视为 variable =“fileLocation”,table =“”,column =“fileLocation”,isSource =

1 个答案:

答案 0 :(得分:1)

如果你这样设置:

variable = {"fileName","fileLocation"},  
            table = "files", 
            column = {"FileName","fileLocation"}, 
            isSource = true

然后变量将成为两个值,因为您将它们定义为String数组。

重要的是,您使用自定义注释所做的只取决于您(在运行时使用它做什么),所以:

  getAnnotation(DBAnnotation.class).variable(); // will return the String array with both values.