{"record":{"id":"3d079026fc03e2c4","repo":"hibernate/hibernate-orm","slug":"the-version-attribute-is-not-declared-or-inherited","errorCode":null,"errorMessage":"The version attribute is not declared or inherited by this type [{}]","messagePattern":"The version attribute is not declared or inherited by this type \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java","lineNumber":272,"sourceCode":"\t}\n\n\tpublic boolean hasDeclaredVersionAttribute() {\n\t\treturn isVersioned && versionAttribute != null;\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <Y> SingularPersistentAttribute<? super J, Y> getVersion(@Nonnull Class<Y> javaType) {\n\t\tif ( hasVersionAttribute() ) {\n\t\t\tfinal var version = findVersionAttribute();\n\t\t\tif ( version != null ) {\n\t\t\t\tcheckType( version, javaType );\n\t\t\t\t@SuppressWarnings(\"unchecked\") // safe, we just checked\n\t\t\t\tfinal var castVersion = (SingularPersistentAttribute<? super J, Y>) version;\n\t\t\t\treturn castVersion;\n\t\t\t}\n\t\t}\n\t\tthrow new IllegalArgumentException(\n\t\t\t\t\"The version attribute is not declared or inherited by this type [\" + getJavaType() + \"]\"\n\t\t);\n\t}\n\n\t@Override\n\t@Nullable\n\tpublic SqmSingularPersistentAttribute<? super J, ?> findVersionAttribute() {\n\t\tif ( versionAttribute != null ) {\n\t\t\treturn versionAttribute;\n\t\t}\n\t\telse if ( getSuperType() != null ) {\n\t\t\treturn getSuperType().findVersionAttribute();\n\t\t}\n\t\telse {\n\t\t\treturn null;\n\t\t}\n\t}\n","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractIdentifiableType.java#L254-L290","documentation":"getVersion(Class) requires an @Version attribute either declared on or inherited by the type; findVersionAttribute() walks the hierarchy and returns null when there is none. Hibernate then throws IllegalArgumentException. Entities with optimistic locking disabled (@OptimisticLocking or simply no @Version field) hit this.","triggerScenarios":"Calling entityType.getVersion(Integer.class) on an entity with no @Version column; generic optimistic-locking helpers that assume every entity is versioned.","commonSituations":"Adding a version-audit utility to a codebase where only some entities are @Version-annotated. Entities relying on timestamp or all-column optimistic locking instead of a version column.","solutions":["Check hasVersionAttribute() (or nullable findVersionAttribute()) before calling getVersion(Class)","Add @Version private Long version; to the entity if optimistic versioning is actually wanted","In generic code treat a missing version as 'no optimistic lock' rather than an error"],"exampleFix":"// before\nvar v = entityType.getVersion(Long.class); // entity has no @Version -> throws\n\n// after\nif (entityType.hasVersionAttribute()) {\n    var v = entityType.getVersion(Long.class);\n} else {\n    // unversioned entity: rely on dirty-check locking\n}","handlingStrategy":"validation","validationCode":"if (identifiableType.hasVersionAttribute()) {\n    var version = identifiableType.getVersion(versionJavaType(identifiableType));\n} else {\n    // unversioned: use dirty-check or all-column optimistic locking\n}","typeGuard":null,"tryCatchPattern":"try {\n    return type.getVersion(cls);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"version attribute is not declared or inherited\")) {\n        return null; // entity is not versioned\n    }\n    throw e;\n}","preventionTips":["Guard getVersion() with hasVersionAttribute()","Prefer nullable findVersionAttribute() in optional contexts","Centralize optimistic-lock support detection per entity in one utility"],"tags":["hibernate","jpa-metamodel","version-attribute","optimistic-locking","illegal-argument"],"backgroundTag":"jpa-metamodel-missing-version","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}