{"record":{"id":"ba1ca3f8fb27f8b3","repo":"hibernate/hibernate-orm","slug":"entity-class-classname-is-annotated-cache","errorCode":null,"errorMessage":"Entity class '${className}' is annotated '@Cache' but it is a subclass in an entity inheritance hierarchy (only root classes may define second-level caching semantics)","messagePattern":"Entity class '(.+?)' is annotated '@Cache' but it is a subclass in an entity inheritance hierarchy \\(only root classes may define second-level caching semantics\\)","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java","lineNumber":1914,"sourceCode":"\t\tisCached = false;\n\t\tcacheConcurrentStrategy = null;\n\t\tcacheRegion = null;\n\t\tcacheLazyProperty = true;\n\t\tqueryCacheLayout = null;\n\t\tif ( isRootEntity() ) {\n\t\t\tbindRootClassCache();\n\t\t}\n\t\telse {\n\t\t\tbindSubclassCache();\n\t\t}\n\t}\n\n\tprivate void bindSubclassCache() {\n\t\tif ( annotatedClass.hasAnnotationUsage( Cache.class, modelsContext() ) ) {\n\t\t\tfinal String className = persistentClass.getClassName() == null\n\t\t\t\t\t? annotatedClass.getName()\n\t\t\t\t\t: persistentClass.getClassName();\n\t\t\tthrow new AnnotationException(\"Entity class '\" + className\n\t\t\t\t\t+  \"' is annotated '@Cache' but it is a subclass in an entity inheritance hierarchy\"\n\t\t\t\t\t+\" (only root classes may define second-level caching semantics)\");\n\t\t}\n\n\t\tfinal var cacheable = annotatedClass.getAnnotationUsage( Cacheable.class, modelsContext() );\n\t\tisCached = cacheable == null && persistentClass.getSuperclass() != null\n\t\t\t\t// we should inherit the root class caching config\n\t\t\t\t? persistentClass.getSuperclass().isCached()\n\t\t\t\t//TODO: is this even correct?\n\t\t\t\t//      Do we even correctly support selectively enabling caching on subclasses like this?\n\t\t\t\t: isCacheable( cacheable );\n\t}\n\n\tprivate void bindRootClassCache() {\n\t\tfinal var sourceModelContext = modelsContext();\n\n\t\tfinal var cache = annotatedClass.getAnnotationUsage( Cache.class, sourceModelContext );\n\t\tfinal var cacheable = annotatedClass.getAnnotationUsage( Cacheable.class, sourceModelContext );","sourceCodeStart":1896,"sourceCodeEnd":1932,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java#L1896-L1932","documentation":"Error \"Entity class '${className}' is annotated '@Cache' but it is a subclass in an entity inheritance hierarchy (only root classes may define second-level caching semantics)\" thrown in hibernate/hibernate-orm.","triggerScenarios":"An annotation restricted to the root of an inheritance hierarchy is placed on a subclass (or the hierarchy rules are otherwise violated).","commonSituations":"SINGLE_TABLE/JOINED hierarchies where @Table, @DiscriminatorColumn, @DiscriminatorFormula, @Cache, @Immutable, @Id or @Version are declared on the wrong class of the hierarchy.","solutions":["Move @Cache to the root class of the entity inheritance hierarchy."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}