{"record":{"id":"ca2f794bca8397b9","repo":"hibernate/hibernate-orm","slug":"error-performing-the-dynamic-instantiation-ca2f79","errorCode":null,"errorMessage":"Error performing the dynamic instantiation","messagePattern":"Error performing the dynamic instantiation","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/BeanInjectorSetter.java","lineNumber":28,"sourceCode":"import org.hibernate.query.sqm.sql.internal.InstantiationException;\n\n/**\n * @author Steve Ebersole\n */\nclass BeanInjectorSetter<T> implements BeanInjector<T> {\n\tprivate final Method setter;\n\n\tpublic BeanInjectorSetter(Method setter) {\n\t\tthis.setter = setter;\n\t}\n\n\t@Override\n\tpublic void inject(T target, Object value) {\n\t\ttry {\n\t\t\tsetter.invoke( target, value );\n\t\t}\n\t\tcatch (InvocationTargetException e) {\n\t\t\tthrow new InstantiationException( \"Error performing the dynamic instantiation\", e.getCause() );\n\t\t}\n\t\tcatch (Exception e) {\n\t\t\tthrow new InstantiationException( \"Error performing the dynamic instantiation\", e );\n\t\t}\n\t}\n}\n","sourceCodeStart":10,"sourceCodeEnd":35,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/BeanInjectorSetter.java#L10-L35","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublic void setTotal(BigDecimal total) {\n    this.total = total.stripTrailingZeros(); // NPE when SUM() is null\n}\n\n// after\npublic void setTotal(BigDecimal total) {\n    this.total = total == null ? null : total.stripTrailingZeros();\n}","handlingStrategy":"try-catch","validationCode":"// At startup, exercise each injection setter with null to prove null-safety\nObject target = SalesDto.class.getDeclaredConstructor().newInstance();\nfor (PropertyDescriptor pd : Introspector.getBeanInfo(SalesDto.class).getPropertyDescriptors()) {\n    if (pd.getWriteMethod() != null) {\n        try { pd.getWriteMethod().invoke(target, new Object[]{null}); }\n        catch (InvocationTargetException ite) {\n            throw new IllegalStateException(\"setter \" + pd.getName()\n                + \" throws on null - will break dynamic instantiation\", ite.getCause());\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    rows = q.getResultList();\n} catch (org.hibernate.query.sqm.sql.internal.InstantiationException e) {\n    if (e.getCause() instanceof NullPointerException) {\n        // your setter received null: fix the setter to be null-safe\n    }\n}","preventionTips":["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."],"tags":["hibernate","orm","dynamic-instantiation","setter-injection","reflection","null-safety"],"backgroundTag":"dynamic-instantiation-injection-failure","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}