hibernate/hibernate-orm · error · InstantiationException

Error performing the dynamic instantiation

Error message

Error performing the dynamic instantiation

What it means

The setter-based counterpart of BeanInjectorField: BeanInjectorSetter.inject() invokes Method.invoke on the property's write method. If the setter itself throws (e.g. NullPointerException on a null argument, IllegalArgumentException, or custom validation inside the setter), the InvocationTargetException is caught and rethrown as InstantiationException('Error performing the dynamic instantiation') with e.getCause() - the exception your setter raised.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/BeanInjectorSetter.java:28

import org.hibernate.query.sqm.sql.internal.InstantiationException;

/**
 * @author Steve Ebersole
 */
class BeanInjectorSetter<T> implements BeanInjector<T> {
	private final Method setter;

	public BeanInjectorSetter(Method setter) {
		this.setter = setter;
	}

	@Override
	public void inject(T target, Object value) {
		try {
			setter.invoke( target, value );
		}
		catch (InvocationTargetException e) {
			throw new InstantiationException( "Error performing the dynamic instantiation", e.getCause() );
		}
		catch (Exception e) {
			throw new InstantiationException( "Error performing the dynamic instantiation", e );
		}
	}
}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Inspect the cause (getCause()/getStackTrace()) - it points at your setter's failing line, not at Hibernate.
  2. Make the setter null-safe and tolerant of the exact types the query produces.
  3. Push transformations out of the setter into the query (cast()/function()) or into an explicit constructor expression.
  4. Add an integration test that executes every dynamic-instantiation query against representative data including nulls.

Example fix

// before
public void setTotal(BigDecimal total) {
    this.total = total.stripTrailingZeros(); // NPE when SUM() is null
}

// after
public void setTotal(BigDecimal total) {
    this.total = total == null ? null : total.stripTrailingZeros();
}
Defensive patterns

Strategy: try-catch

Validate before calling

// At startup, exercise each injection setter with null to prove null-safety
Object target = SalesDto.class.getDeclaredConstructor().newInstance();
for (PropertyDescriptor pd : Introspector.getBeanInfo(SalesDto.class).getPropertyDescriptors()) {
    if (pd.getWriteMethod() != null) {
        try { pd.getWriteMethod().invoke(target, new Object[]{null}); }
        catch (InvocationTargetException ite) {
            throw new IllegalStateException("setter " + pd.getName()
                + " throws on null - will break dynamic instantiation", ite.getCause());
        }
    }
}

Try / catch

try {
    rows = q.getResultList();
} catch (org.hibernate.query.sqm.sql.internal.InstantiationException e) {
    if (e.getCause() instanceof NullPointerException) {
        // your setter received null: fix the setter to be null-safe
    }
}

Prevention

When it happens

Trigger: A dynamic-instantiation query using setter injection where the generated or hand-written setter throws for the injected value: null passed to a setter that does not expect null, range/format parsing inside the setter (e.g. valueOf, date parsing), or explicit argument validation throwing in the setter.

Common situations: Hand-written setters that validate; setters with side effects; null-annotated setters (@NonNull throwing NPE) while the query selects a null value for that alias; setters doing unit conversion that breaks on unexpected input.

Related errors


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