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
- Inspect the cause (getCause()/getStackTrace()) - it points at your setter's failing line, not at Hibernate.
- Make the setter null-safe and tolerant of the exact types the query produces.
- Push transformations out of the setter into the query (cast()/function()) or into an explicit constructor expression.
- 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
- Keep DTO setters trivial: assign only, no parsing/unboxing/conversion.
- Never put @NonNull-throwing validation in a setter used by bean injection.
- Handle null aggregates explicitly: SUM()/MAX() over empty sets return null.
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
- Error performing the dynamic instantiation
- Error instantiating class '{}'
- Error instantiating class '{}' using default constructor: {}
- dynamic instantiation in a sub-query is unsupported
- Cannot set field '{}' to instantiate '{}'
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/ca2f794bca8397b9.
Report an issue: GitHub.