IntelliJ/JDK 11: Cannot find symbol name() interface java.lang.annotation.Annotation

I have this simple code:

String tableName = MyEntity.class.getAnnotation( javax.persistence.Table.class ).name();

But IntelliJ underlines the fact that the method name() does not exist Cannot resolve method "name()".

I also get this error during the compilation:

Error:(29, 68) java: cannot find symbol
  symbol:   method name()
  location: interface java.lang.annotation.Annotation

I saw many example codes using this method, an example here: https://stackoverflow.com/a/1320890/6643803 What am I missing?

Thanks

Answer

As you can see here there is no name() function inside the Annotation interfaces

You probably need to cast it:

Table table = MyEntity.class.getAnnotation(javax.persistence.Table.class);
String tableName = table.name();

Leave a Reply

Your email address will not be published. Required fields are marked *