{"record":{"id":"1f99041f53d721fd","repo":"hibernate/hibernate-orm","slug":"unable-to-locate-constructor-for-embeddable","errorCode":null,"errorMessage":"Unable to locate constructor for embeddable","messagePattern":"Unable to locate constructor for embeddable","errorType":"exception","errorClass":"InstantiationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorPojoStandard.java","lineNumber":55,"sourceCode":"\t\ttry {\n\t\t\treturn getDefaultConstructor( mappedPojoClass );\n\t\t}\n\t\tcatch ( PropertyNotFoundException e ) {\n\t\t\tCORE_LOGGER.noDefaultConstructor( mappedPojoClass.getName() );\n\t\t\treturn null;\n\t\t}\n\t}\n\n\t@Override\n\tpublic Object instantiate(ValueAccess valuesAccess) {\n\t\tif ( isAbstract() ) {\n\t\t\tthrow new InstantiationException(\n\t\t\t\t\t\"Cannot instantiate abstract class or interface\", getMappedPojoClass()\n\t\t\t);\n\t\t}\n\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\tfinal var values = valuesAccess == null ? null : valuesAccess.getValues();\n\t\t\tfinal Object instance = constructor.newInstance();\n\t\t\tif ( values != null ) {\n\t\t\t\t// At this point, createEmptyCompositesEnabled is always true.\n\t\t\t\t// We can only set the property values on the compositeInstance though if there is at least one non null value.\n\t\t\t\t// If the values are all null, we would normally not create a composite instance at all because no values exist.\n\t\t\t\t// Setting all properties to null could cause IllegalArgumentExceptions though when the component has primitive properties.\n\t\t\t\t// To avoid this exception and align with what Hibernate 5 did, we skip setting properties if all values are null.\n\t\t\t\t// A possible alternative could be to initialize the resolved values for primitive fields to their default value,\n\t\t\t\t// but that might cause unexpected outcomes for Hibernate 5 users that use createEmptyCompositesEnabled when updating.\n\t\t\t\t// You can see the need for this by running EmptyCompositeEquivalentToNullTest\n\t\t\t\tembeddableMappingAccess.get().setValues( instance, values );\n\t\t\t}\n\n\t\t\treturn instance;","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EmbeddableInstantiatorPojoStandard.java#L37-L73","documentation":"The standard embeddable instantiator resolves a no-arg constructor via ReflectHelper.getDefaultConstructor at construction time; when it is not found (PropertyNotFoundException), the code logs noDefaultConstructor and leaves this.constructor null. Later, instantiate(ValueAccess) throws InstantiationException(\"Unable to locate constructor for embeddable\") for the mapped class because it has no usable no-arg constructor and no custom/indirecting instantiator was registered.","triggerScenarios":"An @Embeddable class defining only parameterized constructors (no explicit no-arg one), used with @Embedded/@EmbeddedId, where constructor injection was not selected (class not compiled with -parameters, or the mapping did not resolve an instantiator), leaving PojoStandard as the instantiator; private no-arg constructors in classes/package not open to Hibernate can effectively behave the same at lookup time.","commonSituations":"Java classes with only field-populating constructors (e.g. Lombok @AllArgsConstructor without @NoArgsConstructor); Kotlin data classes without a no-arg plugin; Java records accidentally mapped as plain embeddables (no canonical-constructor path selected); value classes designed immutable without a default ctor; JPMS not opening the package so getDefaultConstructor fails.","solutions":["Add an accessible no-arg constructor to the embeddable (it can be protected if the class and package are open to Hibernate; public is safest).","With Lombok: add @NoArgsConstructor (optionally @AllArgsConstructor + @NoArgsConstructor(force = true) for final fields).","Or keep immutable classes and register injection explicitly: compile with -parameters so the indirecting instantiator is used, or supply @EmbeddableInstantiator(MyInstantiator.class).","For records, rely on the record instantiator path (ensure the class really is a record) rather than expecting a no-arg constructor."],"exampleFix":"// before\n@Embeddable\npublic class Period {\n    private final LocalDate start;\n    private final LocalDate end;\n    public Period(LocalDate start, LocalDate end) { ... } // no no-arg ctor\n}\n\n// after (Lombok)\n@Embeddable\n@NoArgsConstructor(force = true)\n@AllArgsConstructor\npublic class Period {\n    private LocalDate start;\n    private LocalDate end;\n}","handlingStrategy":"type-guard","validationCode":"// before bootstrap: every embeddable needs a no-arg ctor OR an explicit instantiator\nstatic void checkNoArgCtor(Class<?> embeddable) {\n    try {\n        Constructor<?> c = embeddable.getDeclaredConstructor();\n        if (!Modifier.isPublic(c.getModifiers()) && !c.canAccess(null))\n            throw new IllegalStateException(\"no-arg ctor of \" + embeddable + \" not accessible\");\n    } catch (NoSuchMethodException e) {\n        throw new IllegalStateException(\"Embeddable missing no-arg constructor: \" + embeddable.getName(), e);\n    }\n}","typeGuard":"static boolean hasAccessibleNoArgConstructor(Class<?> embeddable) {\n    try {\n        Constructor<?> c = embeddable.getDeclaredConstructor();\n        c.setAccessible(true);\n        return true;\n    } catch (NoSuchMethodException | InaccessibleObjectException e) {\n        return false;\n    }\n}","tryCatchPattern":null,"preventionTips":["Add @NoArgsConstructor (Lombok) or an explicit no-arg constructor to every @Embeddable.","For Kotlin, apply the kotlin-noarg / all-open plugin for embeddables.","Alternatively compile with -parameters and use constructor injection or @EmbeddableInstantiator."],"tags":["hibernate","embeddable","no-arg-constructor","instantiation","orm-mapping"],"backgroundTag":"missing-no-arg-constructor","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}