{"record":{"id":"45242e5164c1a6d4","repo":"hibernate/hibernate-orm","slug":"could-not-create-statemanagement","errorCode":null,"errorMessage":"Could not create StateManagement","messagePattern":"Could not create StateManagement","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/mapping/Stateful.java","lineNumber":57,"sourceCode":"\n\tboolean isPrimaryKeyDisabled();\n\n\tvoid setPrimaryKeyDisabled(boolean disabled);\n\n\tdefault StateManagement getStateManagement() {\n\t\tfinal var stateManagementType = getStateManagementType();\n\t\tif ( stateManagementType == null ) {\n\t\t\treturn StandardStateManagement.INSTANCE;\n\t\t}\n\t\telse {\n\t\t\ttry {\n\t\t\t\treturn (StateManagement)\n\t\t\t\t\t\tstateManagementType\n\t\t\t\t\t\t\t\t.getDeclaredField( \"INSTANCE\" )\n\t\t\t\t\t\t\t\t.get( null );\n\t\t\t}\n\t\t\tcatch (IllegalAccessException | NoSuchFieldException e) {\n\t\t\t\tthrow new InstantiationException( \"Could not create StateManagement\",\n\t\t\t\t\t\tstateManagementType, e );\n\t\t\t}\n\t\t}\n\t}\n}\n","sourceCodeStart":39,"sourceCodeEnd":63,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/mapping/Stateful.java#L39-L63","documentation":"Stateful.getStateManagement() (Hibernate 7.4+) resolves the customized state-management strategy for a mapping by reflective convention: the configured Class<? extends StateManagement> must expose a public static INSTANCE field holding the singleton. The throw happens when reflection on that class throws IllegalAccessException or NoSuchFieldException — i.e. the class has no INSTANCE field, or the field exists but is not accessible.","triggerScenarios":"Calling setStateManagementType(MyStateManagement.class) with a class that omits `public static StateManagement INSTANCE`, or declares it private/package-private/final-inaccessible, or is in another module/package not open to Hibernate; then triggering getStateManagement() during runtime-metamodel building for a @SoftDelete/@Audited-style stateful mapping.","commonSituations":"Writing a custom state management strategy and initializing it eagerly versus using the INSTANCE singleton convention; a refactor that renamed the field; JPMS/module boundaries (IllegalAccessException: module does not open package); copy of StandardStateManagement that drops the field.","solutions":["Add the required singleton to the custom class: `public static final StateManagement INSTANCE = new MyStateManagement();`.","Ensure the field is public, static, non-final-accessible (or open the module/package if under JPMS) so stateManagementType.getDeclaredField(\"INSTANCE\").get(null) succeeds.","If a per-instance strategy is required, implement StateManagement as an enum with an INSTANCE constant or register via the extension point that constructs instances rather than the INSTANCE convention.","Remove the setStateManagementType call to fall back to StandardStateManagement.INSTANCE if customization was accidental."],"exampleFix":"// before\npublic final class MyStateManagement implements StateManagement {\n    // no INSTANCE field -> InstantiationException(\"Could not create StateManagement\")\n}\n\n// after\npublic final class MyStateManagement implements StateManagement {\n    public static final MyStateManagement INSTANCE = new MyStateManagement();\n    // strategy methods...\n}","handlingStrategy":"validation","validationCode":"// before registering a custom state management type, verify the INSTANCE convention\nField f = MyStateManagement.class.getDeclaredField(\"INSTANCE\");\nassert Modifier.isStatic(f.getModifiers()) && Modifier.isPublic(f.getModifiers());\nassert StateManagement.class.isAssignableFrom(f.getType());","typeGuard":"static boolean hasStateManagementInstance(Class<? extends StateManagement> type) {\n    try {\n        Field f = type.getDeclaredField(\"INSTANCE\");\n        return Modifier.isStatic(f.getModifiers())\n                && StateManagement.class.isAssignableFrom(f.getReturnType());\n    } catch (NoSuchFieldException e) {\n        return false;\n    }\n}","tryCatchPattern":"try {\n    persistentClass.setStateManagementType(MyStateManagement.class);\n} catch (RuntimeException e) {\n    // InstantiationException(\"Could not create StateManagement\") — check INSTANCE field presence/visibility\n    throw e;\n}","preventionTips":["Model custom strategies on StandardStateManagement: public static final <Type> INSTANCE field.","Add a unit test that reflectively checks every custom StateManagement exposes a public static INSTANCE.","Under JPMS, open the package containing the strategy to hibernate.core."],"tags":["hibernate","state-management","reflection","instance-field","orm-mapping"],"backgroundTag":"missing-static-instance-field","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}