Home > Enterprise >  Annotation type not applicable to this kind of declaration error in package info class in Java 8
Annotation type not applicable to this kind of declaration error in package info class in Java 8

Time:09-29

I have a package info class, it used to compile in Java 6/7. But in Java 8, I get compilation errors:

*****error: annotation type not applicable to this kind of declaration @NamedNativeQueries({

error: annotation type not applicable to this kind of declaration @SqlResultSetMappings({*****

This is the code:

@NamedNativeQueries({

    @NamedNativeQuery(name = "",
        query = "",
        resultSetMapping = "mapping"
    ),
    NamedNativeQuery(
         name = "",
         query = "",
         callable = true,
         readOnly = false,
         resultSetMapping = ""
    )
})

@SqlResultSetMappings({

})

package abc.domain;
import javax.persistence.ColumnResult;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;

Thanks in advance for your help

CodePudding user response:

EDIT: Additional comments have made more clear what's going wrong here; as a consequence, the nature of this answer has changed somewhat.

It sounds like you switched from Hibernate's own version of @NamedNativeQueries and company, which you can stick on packages, to the general javax persistence variant, which cannot be placed on packages.

You must have removed and re-generated the imports in your attempt to convert this code. Don't do that - remove all those imports and replace them with import org.hibernate.annotations.NamedNativeQueries and friends instead.


For posterity, the original answer, still valid but only in context specifically for javax.persistence.NamedNativeQueries.

You must be misremembering; it does not and never has worked on java7 (that's the java7 docs of amedNativeQueries - note how it has a @Target(value=TYPE) marker, so it cannot be put on a package, and that's the v7 edition of the docs!)

You put such things on a type, for example the top level type. Which means the annotations appear near the top, but after the package statement. Given that you're using them in a package-info.java file, they simply cannot appear here (and never could).

SqlResultSetMappings is the same.

There's a small chance that somehow javac7 didn't actually check the Target condition of these annotations. However, that simply means that your code never worked, even if it did compile.

  • Related