{"record":{"id":"2881fc998e7f91d9","repo":"hibernate/hibernate-orm","slug":"property-property-is-annotated-optimisticlo-2881fc","errorCode":null,"errorMessage":"Property '${property}' is annotated '@OptimisticLock(excluded=true)' and '@Id'","messagePattern":"Property '(.+?)' is annotated '@OptimisticLock\\(excluded=true\\)' and '@Id'","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java","lineNumber":684,"sourceCode":"\t}\n\n\tprivate void checkAnnotation(Class<? extends Annotation> annotationClass, Class<?> propertyType) {\n\t\tif ( memberDetails.hasDirectAnnotationUsage( annotationClass )\n\t\t\t\t&& !memberDetails.getType().isImplementor( propertyType ) ) {\n\t\t\tthrow new AnnotationException( \"Property '\" + qualify( holder.getPath(), name )\n\t\t\t\t\t+ \"' is annotated '@\" + annotationClass.getSimpleName()\n\t\t\t\t\t+ \"' but is not of type '\" + propertyType.getTypeName() + \"'\" );\n\t\t}\n\t}\n\n\tprivate void validateOptimisticLock(boolean excluded) {\n\t\tif ( excluded ) {\n\t\t\tif ( isVersion( memberDetails ) ) {\n\t\t\t\tthrow new AnnotationException(\"Property '\" + qualify( holder.getPath(), name )\n\t\t\t\t\t\t+ \"' is annotated '@OptimisticLock(excluded=true)' and '@Version'\" );\n\t\t\t}\n\t\t\tif ( isSimpleId( memberDetails ) ) {\n\t\t\t\tthrow new AnnotationException(\"Property '\" + qualify( holder.getPath(), name )\n\t\t\t\t\t\t+ \"' is annotated '@OptimisticLock(excluded=true)' and '@Id'\" );\n\t\t\t}\n\t\t\tif ( isEmbeddedId( memberDetails ) ) {\n\t\t\t\tthrow new AnnotationException( \"Property '\" + qualify( holder.getPath(), name )\n\t\t\t\t\t\t+ \"' is annotated '@OptimisticLock(excluded=true)' and '@EmbeddedId'\" );\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * @param elements List of {@link PropertyData} instances\n\t * @param propertyContainer Metadata about a class and its properties\n\t * @param idPropertyCounter number of id properties already present in list of {@link PropertyData} instances\n\t *\n\t * @return total number of id properties found after iterating the elements of {@code annotatedClass}\n\t * using the determined access strategy (starting from the provided {@code idPropertyCounter})\n\t */\n\tstatic int addElementsOfClass(","sourceCodeStart":666,"sourceCodeEnd":702,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java#L666-L702","documentation":"@OptimisticLock(excluded = true) on the identifier property would exclude the id from optimistic-lock checks, which Hibernate forbids because identifier participation in locking is fixed by the mapping, not by this flag. PropertyBinder throws AnnotationException when it finds @Id together with excluded=true.","triggerScenarios":"A simple @Id property also annotated @OptimisticLock(excluded = true); typically an over-broad copy-paste of the exclusion flag onto every field of an entity.","commonSituations":"Applying @OptimisticLock(excluded=true) wholesale via code templates; attempting to reduce lock-scope overhead by excluding the id.","solutions":["Remove @OptimisticLock(excluded = true) from the @Id property.","Apply excluded=true only to genuinely lock-irrelevant mutable fields (e.g. audit counters, last-login timestamps)."],"exampleFix":"// before\n@Id\n@GeneratedValue(strategy = GenerationType.IDENTITY)\n@OptimisticLock(excluded = true)\nprivate Long id;\n\n// after\n@Id\n@GeneratedValue(strategy = GenerationType.IDENTITY)\nprivate Long id;","handlingStrategy":"validation","validationCode":"for (Class<?> entity : annotatedClasses) {\n    for (Field f : entity.getDeclaredFields()) {\n        OptimisticLock ol = f.getAnnotation(OptimisticLock.class);\n        if (ol != null && ol.excluded() && f.isAnnotationPresent(Id.class)) {\n            throw new IllegalStateException(\"@Id property may not use @OptimisticLock(excluded=true): \" + f);\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    SessionFactory sf = cfg.buildSessionFactory();\n} catch (AnnotationException e) {\n    throw new IllegalStateException(\"Optimistic-lock annotation conflict: \" + e.getMessage(), e);\n}","preventionTips":["Keep @OptimisticLock(excluded=true) off identifier fields","Prefer applying the flag via careful review rather than bulk templates"],"tags":["hibernate","jpa","optimistic-lock","id","annotation-conflict","bootstrap"],"backgroundTag":"optimistic-lock-exclusion-conflict","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}