hibernate/hibernate-orm · error · InstantiationException

Could not create StateManagement

Error message

Could not create StateManagement

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/mapping/Stateful.java:57

	boolean isPrimaryKeyDisabled();

	void setPrimaryKeyDisabled(boolean disabled);

	default StateManagement getStateManagement() {
		final var stateManagementType = getStateManagementType();
		if ( stateManagementType == null ) {
			return StandardStateManagement.INSTANCE;
		}
		else {
			try {
				return (StateManagement)
						stateManagementType
								.getDeclaredField( "INSTANCE" )
								.get( null );
			}
			catch (IllegalAccessException | NoSuchFieldException e) {
				throw new InstantiationException( "Could not create StateManagement",
						stateManagementType, e );
			}
		}
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Add the required singleton to the custom class: `public static final StateManagement INSTANCE = new MyStateManagement();`.
  2. Ensure the field is public, static, non-final-accessible (or open the module/package if under JPMS) so stateManagementType.getDeclaredField("INSTANCE").get(null) succeeds.
  3. 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.
  4. Remove the setStateManagementType call to fall back to StandardStateManagement.INSTANCE if customization was accidental.

Example fix

// before
public final class MyStateManagement implements StateManagement {
    // no INSTANCE field -> InstantiationException("Could not create StateManagement")
}

// after
public final class MyStateManagement implements StateManagement {
    public static final MyStateManagement INSTANCE = new MyStateManagement();
    // strategy methods...
}
Defensive patterns

Strategy: validation

Validate before calling

// before registering a custom state management type, verify the INSTANCE convention
Field f = MyStateManagement.class.getDeclaredField("INSTANCE");
assert Modifier.isStatic(f.getModifiers()) && Modifier.isPublic(f.getModifiers());
assert StateManagement.class.isAssignableFrom(f.getType());

Type guard

static boolean hasStateManagementInstance(Class<? extends StateManagement> type) {
    try {
        Field f = type.getDeclaredField("INSTANCE");
        return Modifier.isStatic(f.getModifiers())
                && StateManagement.class.isAssignableFrom(f.getReturnType());
    } catch (NoSuchFieldException e) {
        return false;
    }
}

Try / catch

try {
    persistentClass.setStateManagementType(MyStateManagement.class);
} catch (RuntimeException e) {
    // InstantiationException("Could not create StateManagement") — check INSTANCE field presence/visibility
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/45242e5164c1a6d4. Report an issue: GitHub.