{"record":{"id":"c39dc2ee9664bf89","repo":"hibernate/hibernate-orm","slug":"could-not-instantiate-entity-c39dc2","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/EmbeddableInstantiatorRecordStandard.java","lineNumber":38,"sourceCode":"\n\tprotected final Constructor<?> constructor;\n\n\tpublic EmbeddableInstantiatorRecordStandard(Class<?> javaType) {\n\t\tsuper( javaType );\n\t\tconstructor = getConstructorOrNull( javaType, getRecordComponentTypes( javaType ) );\n\t}\n\n\t@Override\n\tpublic Object instantiate(ValueAccess valuesAccess) {\n\t\tif ( constructor == null ) {\n\t\t\tthrow new InstantiationException( \"Unable to locate constructor for embeddable\", getMappedPojoClass() );\n\t\t}\n\n\t\ttry {\n\t\t\treturn constructor.newInstance( valuesAccess.getValues() );\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","sourceCodeStart":20,"sourceCodeEnd":42,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorRecordStandard.java#L20-L42","documentation":"Thrown by EmbeddableInstantiatorRecordStandard.instantiate when invoking the record's canonical constructor with the values read from the database fails. Unlike the indirecting variants, this standard instantiator passes valuesAccess.getValues() straight to the constructor, so any exception raised inside the record constructor (null validation, invalid values, arithmetic on nulls) or any reflective access problem surfaces here wrapped as InstantiationException with the original cause.","triggerScenarios":"Loading an @Embedded record whose compact constructor throws on data present in the row (nulls, out-of-range values); primitive record components receiving null columns; record defined in a named module not opened to Hibernate, causing IllegalAccessException from Constructor.newInstance.","commonSituations":"Validation inside record constructors (requireNonNull, range checks) clashing with real production data; nullable legacy columns mapped to strictly-typed records; strict JPMS encapsulation after modularizing the domain model.","solutions":["Unwrap the cause of the InstantiationException to find the exact constructor line that threw","Relax the record constructor for values Hibernate may legitimately pass (nulls for nullable columns) or make the columns NOT NULL","Use wrapper/object types for components backed by nullable columns","Add 'opens <package> to hibernate.core' in module-info when entities live in a named module"],"exampleFix":"// before\npublic record Period(LocalDate start, LocalDate end) {\n    public Period {                          // throws on historical rows\n        Objects.requireNonNull(end);\n    }\n}\n\n// after\npublic record Period(LocalDate start, LocalDate end) {\n    public Period {\n        // end may be null for open-ended periods - validate at use site instead\n    }\n}","handlingStrategy":"try-catch","validationCode":"// Detect record components that cannot handle null before going to production\nfor ( RecordComponent rc : Period.class.getRecordComponents() ) {\n    if ( rc.getType().isPrimitive() && columnAllowsNull(rc.getName()) ) {\n        throw new IllegalStateException(\"Primitive component \" + rc.getName() + \" on nullable column\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return session.get(Contract.class, id);\n}\ncatch ( org.hibernate.InstantiationException e ) {\n    Throwable root = e.getCause() != null ? e.getCause() : e;\n    log.error(\"Record constructor for {} failed: {}\", e.getMessage(), root);\n    throw new IllegalStateException(\"Stored data rejected by embeddable record\", root);\n}","preventionTips":["Keep record constructors lenient toward values the database can contain","Push invariants to @PrePersist/@PreUpdate service-layer validation, not record constructors","Under JPMS, open entity packages to hibernate.core to keep Constructor.newInstance legal"],"tags":["hibernate","jpa","embeddable","record","instantiation","null-handling"],"backgroundTag":"jpa-embeddable-instantiation-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}