{"record":{"id":"cb68d02cc29c7734","repo":"hibernate/hibernate-orm","slug":"generatedvalue-annotation-specified-strategy-g","errorCode":null,"errorMessage":"@GeneratedValue annotation specified 'strategy=${generationType}' and 'generator' but the generator name is unnecessary","messagePattern":"@GeneratedValue annotation specified 'strategy=(.+?)' and 'generator' but the generator name is unnecessary","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/IdentifierGeneratorDefinition.java","lineNumber":112,"sourceCode":"\n\t@Internal\n\tpublic static IdentifierGeneratorDefinition createImplicit(\n\t\t\tString name,\n\t\t\tTypeDetails idType,\n\t\t\tString generatorName,\n\t\t\tGenerationType generationType) {\n\t\t// If we were unable to locate an actual matching named generator assume\n\t\t// a sequence/table of the given name, make one based on GenerationType.\n\n\t\treturn switch ( generationType == null ? GenerationType.SEQUENCE : generationType ) {\n\t\t\tcase SEQUENCE -> buildSequenceGeneratorDefinition( name );\n\t\t\tcase TABLE -> buildTableGeneratorDefinition( name );\n\t\t\tcase AUTO -> new IdentifierGeneratorDefinition(\n\t\t\t\t\tname,\n\t\t\t\t\tgeneratorStrategy( generationType, generatorName, idType ),\n\t\t\t\t\tsingletonMap( IdentifierGenerator.GENERATOR_NAME, name )\n\t\t\t);\n\t\t\tcase IDENTITY, UUID -> throw new AnnotationException(\n\t\t\t\t\t\"@GeneratedValue annotation specified 'strategy=\" + generationType\n\t\t\t\t\t+ \"' and 'generator' but the generator name is unnecessary\"\n\t\t\t);\n\t\t};\n\t}\n\n\tprivate static IdentifierGeneratorDefinition buildTableGeneratorDefinition(String name) {\n\t\tfinal TableGeneratorJpaAnnotation tableGeneratorUsage = TABLE_GENERATOR.createUsage( null );\n\t\tif ( isNotEmpty( name ) ) {\n\t\t\ttableGeneratorUsage.name( name );\n\t\t}\n\t\tfinal Builder builder = new Builder();\n\t\tinterpretTableGenerator( tableGeneratorUsage, builder );\n\t\treturn builder.build();\n\t}\n\n\tprivate static IdentifierGeneratorDefinition buildSequenceGeneratorDefinition(String name) {\n\t\tfinal SequenceGeneratorJpaAnnotation sequenceGeneratorUsage = SEQUENCE_GENERATOR.createUsage( null );","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/IdentifierGeneratorDefinition.java#L94-L130","documentation":"IdentifierGeneratorDefinition.buildIdentifierGenerator throws AnnotationException when @GeneratedValue combines GenerationType.IDENTITY or GenerationType.UUID with a non-empty generator name. For these strategies Hibernate derives the generator entirely from the strategy, so a named generator is meaningless and is rejected as a mapping error rather than ignored.","triggerScenarios":"An entity or mapped-superclass id annotated like @GeneratedValue(strategy = GenerationType.IDENTITY, generator = \"myGen\") or @GeneratedValue(strategy = GenerationType.UUID, generator = \"uuidGen\"). The code path runs when no registered generator definition with that name exists, so Hibernate tries to fabricate one from the strategy and hits the forbidden combination.","commonSituations":"Copy-paste from sequence-style entities; generators templated by code generators (JHipster-likes, internal scaffolds) that always emit a generator name; code migrated from older Hibernate versions that silently tolerated the extra attribute.","solutions":["Delete the generator attribute for IDENTITY and UUID strategies","If you actually want a named generator, switch the strategy to SEQUENCE/TABLE and define it (@SequenceGenerator/@TableGenerator or generator definition)","Audit other entities for the same pattern — one fixed annotation usually means siblings have it too"],"exampleFix":"// before\n@Id\n@GeneratedValue(strategy = GenerationType.IDENTITY, generator = \"item_gen\")\nprivate Long id;\n\n// after\n@Id\n@GeneratedValue(strategy = GenerationType.IDENTITY)\nprivate Long id;","handlingStrategy":"validation","validationCode":"for (Field f : entity.getDeclaredFields()) {\n    GeneratedValue gv = f.getAnnotation(GeneratedValue.class);\n    if (gv != null && !gv.generator().isEmpty()\n            && (gv.strategy() == GenerationType.IDENTITY || gv.strategy() == GenerationType.UUID)) {\n        throw new IllegalStateException(\"@GeneratedValue on \" + f + \" must not name a generator for \" + gv.strategy());\n    }\n}","typeGuard":null,"tryCatchPattern":"catch (AnnotationException e) {\n    throw new IllegalStateException(\"Remove the generator attribute from @GeneratedValue(strategy=IDENTITY/UUID) ids\", e);\n}","preventionTips":["Write a build-time ArchUnit/reflection test enforcing the rule across all entities","Do not copy @GeneratedValue blocks between sequence and identity entities"],"tags":["jpa","annotations","generatedvalue","id-mapping"],"backgroundTag":"invalid-annotation-combination","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}