{"record":{"id":"5590b29d34a73daa","repo":"hibernate/hibernate-orm","slug":"cannot-instantiate-class-it-has-no-construct-5590b2","errorCode":null,"errorMessage":"Cannot instantiate class '{}' (it has no constructor with signature {}, and has arguments with duplicate aliases [{}])","messagePattern":"Cannot instantiate class '(.+?)' \\(it has no constructor with signature (.+?), and has arguments with duplicate aliases \\[(.+?)\\]\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationResultImpl.java","lineNumber":188,"sourceCode":"\t\t\treturn new DynamicInstantiationAssemblerConstructorImpl<>( constructor, javaType, argumentReaders );\n\t\t}\n\n\t\tif ( LOG.isDebugEnabled() ) {\n\t\t\tLOG.debugf(\n\t\t\t\t\t\"Could not locate appropriate constructor for dynamic instantiation of [%s]; attempting bean-injection instantiation\",\n\t\t\t\t\tjavaType.getTypeName()\n\t\t\t);\n\t\t}\n\n\t\tif ( !areAllArgumentsAliased) {\n\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\"Cannot instantiate class '\" + javaType.getTypeName() + \"'\"\n\t\t\t\t\t\t\t+ \" (it has no constructor with signature \" + signature()\n\t\t\t\t\t\t\t+ \", and not every argument has an alias)\"\n\t\t\t);\n\t\t}\n\t\tif ( !duplicatedAliases.isEmpty() ) {\n\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\"Cannot instantiate class '\" + javaType.getTypeName() + \"'\"\n\t\t\t\t\t\t\t+ \" (it has no constructor with signature \" + signature()\n\t\t\t\t\t\t\t+ \", and has arguments with duplicate aliases [\"\n\t\t\t\t\t\t\t+ StringHelper.join( \",\", duplicatedAliases) + \"])\"\n\t\t\t);\n\t\t}\n\n\t\treturn new DynamicInstantiationAssemblerInjectionImpl<>( javaType, argumentReaders );\n\t}\n\n\tprivate static Class<?> argumentClass(ArgumentReader<?> reader) {\n\t\tfinal var assembledJavaType = reader.getAssembledJavaType();\n\t\treturn assembledJavaType instanceof DateJavaType temporalJavaType\n\t\t\t\t// Hack to accommodate a constructor with java.sql parameter\n\t\t\t\t// types when the entity has java.util.Date as its field types.\n\t\t\t\t// (This was requested in HHH-4179 and we fixed it by accident.)\n\t\t\t\t? TemporalJavaType.resolveJavaTypeClass( temporalJavaType.getPrecision() )\n\t\t\t\t: assembledJavaType.getJavaTypeClass();","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiationResultImpl.java#L170-L206","documentation":"Thrown for `select new com.acme.Dto(...)` dynamic instantiation when no constructor matches the selected argument types and the alias-based bean-injection fallback cannot be used because two or more arguments share the same alias. The message lists the duplicated aliases. Injection needs each selected value bound to a distinct field/setter name, so duplicate aliases block it.","triggerScenarios":"`select new org.acme.Dto(a.id as x, b.id as x)` where Dto also lacks a matching constructor; refactoring that introduced a duplicate alias into a DTO instantiation that previously relied on injection; generated HQL reusing one alias name across joined tables.","commonSituations":"Copy-pasted select lines in DTO instantiations; large DTO projections where duplicate aliases go unnoticed; combination of a missing constructor plus non-unique aliases after a model change.","solutions":["Make all aliases unique (e.g. `as orderId`, `as customerId`) so injection can map each value to a distinct property","Preferably add an exact-match constructor for the selected types, which makes aliases irrelevant","Check the exception message for the exact list of duplicated aliases and the constructor signature Hibernate wanted"],"exampleFix":"// before\nselect new org.acme.OrderDto(o.id as id, c.id as id) from Order o join o.customer c\n// after\nselect new org.acme.OrderDto(o.id as orderId, c.id as customerId) from Order o join o.customer c","handlingStrategy":"validation","validationCode":"// Ensure unique aliases so the injection fallback stays available\nSet<String> s = new HashSet<>();\nif (!s.addAll(Arrays.asList(\"orderId\", \"customerId\"))) throw new IllegalArgumentException(\"duplicate alias\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefix aliases with the entity name to avoid collisions in joined projections","Prefer exact-match constructors over alias-based injection for DTOs - fewer moving parts","The exception lists the duplicated aliases and the wanted signature; fix both in one pass"],"tags":["hql","dynamic-instantiation","dto","duplicate-alias","bean-injection"],"backgroundTag":"hql-dynamic-instantiation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}