{"record":{"id":"9b13e86976cd1961","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-entity","errorCode":null,"errorMessage":"Could not instantiate entity","messagePattern":"Could not instantiate entity","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorPojoIndirecting.java","lineNumber":52,"sourceCode":"\t\t}\n\t\tfinal var index = new int[componentNames.length];\n\t\treturn EmbeddableHelper.resolveIndex( propertyNames, componentNames, index )\n\t\t\t\t? new EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index )\n\t\t\t\t: new EmbeddableInstantiatorPojoIndirecting( constructor, index );\n\t}\n\n\t@Override\n\tpublic Object instantiate(ValueAccess valuesAccess) {\n\t\ttry {\n\t\t\tfinal var originalValues = valuesAccess.getValues();\n\t\t\tfinal var values = new Object[originalValues.length];\n\t\t\tfor ( int i = 0; i < values.length; i++ ) {\n\t\t\t\tvalues[i] = originalValues[index[i]];\n\t\t\t}\n\t\t\treturn constructor.newInstance( values );\n\t\t}\n\t\tcatch ( Exception e ) {\n\t\t\tthrow new InstantiationException( \"Could not instantiate entity\", getMappedPojoClass(), e );\n\t\t}\n\t}\n\n\t// Handles gaps, by leaving the value null for that index\n\tprivate static class EmbeddableInstantiatorPojoIndirectingWithGap extends EmbeddableInstantiatorPojoIndirecting {\n\n\t\tpublic EmbeddableInstantiatorPojoIndirectingWithGap(Constructor<?> constructor, int[] index) {\n\t\t\tsuper( constructor, index );\n\t\t}\n\n\t\t@Override\n\t\tpublic Object instantiate(ValueAccess valuesAccess) {\n\t\t\ttry {\n\t\t\t\tfinal var originalValues = valuesAccess.getValues();\n\t\t\t\tfinal var values = new Object[index.length];\n\t\t\t\tfor ( int i = 0; i < values.length; i++ ) {\n\t\t\t\t\tfinal int index = this.index[i];\n\t\t\t\t\tif ( index >= 0 ) {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorPojoIndirecting.java#L34-L70","documentation":"The POJO indirecting instantiator creates embeddable instances by reordering ValueAccess values per the constructor-to-property index and calling constructor.newInstance(values). Any Exception from that call — constructor throws, wrong argument types, IllegalAccessException, InvocationTargetException — is wrapped into InstantiationException(\"Could not instantiate entity\") naming the mapped class and carrying the original cause.","triggerScenarios":"Calling instantiate directly or loading/persisting an entity whose embeddable uses constructor injection where: the constructor throws (NPE, validation), an argument type mismatches a parameter (e.g. property resolved as a different wrapper/enum), or the constructor is not accessible to Hibernate (private in a non-opened package/module).","commonSituations":"Embeddable constructors that validate arguments and reject nulls during LEFT JOIN / empty-composite creation; type drift after changing a property type without updating the constructor signature; Java modules (JPMS) not opening the embeddable package to Hibernate; Kotlin/Scala classes with constructors requiring non-null primitives receiving null during outer-join null-row instantiation.","solutions":["Read the cause chain (getCause()) — the real exception is the constructor's own failure, not this wrapper.","Make the constructor tolerant of nulls during instantiation (defer validation to lifecycle callbacks or accept nullable params as primitives' defaults).","Keep constructor parameter types aligned with the mapped property types after refactors.","Ensure the constructor is accessible (public, or module opened to hibernate) so newInstance is legal."],"exampleFix":"// before\npublic class Address {\n    public Address(String city, String zip) {\n        Objects.requireNonNull(city); // throws on null-row join instantiation\n        ...\n    }\n}\n\n// after\npublic class Address {\n    public Address(String city, String zip) {\n        this.city = city; // tolerate nulls; validate on persist if needed\n        this.zip = zip;\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    OrderLine line = session.find(OrderLine.class, pk);\n} catch (InstantiationException e) {\n    if (\"Could not instantiate entity\".equals(e.getMessage())) {\n        Throwable cause = e.getCause(); // the constructor's real exception — fix that first\n        log.error(\"Embeddable constructor failed for {}\", e.getClassName(), cause);\n    }\n    throw e;\n}","preventionTips":["Keep embeddable constructors null-tolerant; validation belongs in @PrePersist/@PreUpdate.","After changing property types, update constructor parameter types in the same commit.","Under JPMS, open the embeddable's package to hibernate.core."],"tags":["hibernate","embeddable","instantiation","constructor-injection","reflection"],"backgroundTag":"embeddable-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}