{"record":{"id":"fada92507bd48435","repo":"hibernate/hibernate-orm","slug":"dynamic-instantiation-in-a-sub-query-is-unsupporte","errorCode":null,"errorMessage":"dynamic instantiation in a sub-query is unsupported","messagePattern":"dynamic instantiation in a sub-query is unsupported","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiation.java","lineNumber":111,"sourceCode":"\t}\n\n\t@Override\n\tpublic DomainResult<T> createDomainResult(\n\t\t\tString resultVariable,\n\t\t\tDomainResultCreationState creationState) {\n\t\treturn new DynamicInstantiationResultImpl<>(\n\t\t\t\tresultVariable,\n\t\t\t\tgetNature(),\n\t\t\t\tgetTargetJavaType(),\n\t\t\t\tgetArguments().stream()\n\t\t\t\t\t\t.map( argument -> argument.buildArgumentDomainResult( creationState ) )\n\t\t\t\t\t\t.collect( toList() )\n\t\t);\n\t}\n\n\t@Override\n\tpublic void applySqlSelections(DomainResultCreationState creationState) {\n\t\tthrow new UnsupportedOperationException( \"dynamic instantiation in a sub-query is unsupported\" );\n\t}\n\n//\n//\t@SuppressWarnings(\"unchecked\")\n//\tprivate static DomainResultAssembler resolveAssembler(\n//\t\t\tDynamicInstantiation dynamicInstantiation,\n//\t\t\tboolean areAllArgumentsAliased,\n//\t\t\tboolean areAnyArgumentsAliased,\n//\t\t\tList<String> duplicatedAliases,\n//\t\t\tList<ArgumentReader<?>> argumentReaders,\n//\t\t\tAssemblerCreationState creationState) {\n//\n//\t\tif ( dynamicInstantiation.getNature() == DynamicInstantiationNature.LIST ) {\n//\t\t\tif ( LOG.isDebugEnabled() && areAnyArgumentsAliased ) {\n//\t\t\t\tLOG.debug( \"One or more arguments for List dynamic instantiation (`new list(...)`) specified an alias; ignoring\" );\n//\t\t\t}\n//\t\t\treturn new DynamicInstantiationListAssemblerImpl(\n//\t\t\t\t\t(JavaType<List>) dynamicInstantiation.getTargetJavaType(),","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/instantiation/internal/DynamicInstantiation.java#L93-L129","documentation":"DynamicInstantiation models 'select new X(...) ...' (constructor or list/map injection) in the SELECT clause. It supports building a DomainResult for the top-level query, but when the dynamic instantiation ends up inside a sub-query, applySqlSelections() is called on it and it throws UnsupportedOperationException('dynamic instantiation in a sub-query is unsupported') - Hibernate cannot propagate instantiation targets through the subquery boundary into SQL select items.","triggerScenarios":"Using a constructor expression / dynamic instantiation inside a subquery, e.g. `where o.id in (select new com.acme.Pair(o2.id, o2.code) from Order o2)` or criteria subquery selections; dynamic instantiation nested in a query used as a derived table or in processing paths that require SQL selections from the subquery.","commonSituations":"Copy-pasting a working top-level projection into an exists/in subquery; criteria queries reusing a selection inside SubQuery expression lists; upgrading queries that worked in older 5.x builds where the restriction was less strict.","solutions":["Move the instantiation to the outer query: the subquery selects plain values (ids, scalars) and the outer select wraps them.","Replace the subquery with a join so no instantiation is needed inside it.","Fetch the raw subquery results first, then build the objects in Java (stream + map to DTO).","Check the Hibernate JIRA and latest 6.x patch - restrictions around instantiation positions have shifted across releases."],"exampleFix":"// before\nem.createQuery(\"select o from Order o where o.id in \" +\n  \"(select new com.acme.OrderRef(o2.id, o2.code) from Order o2 where o2.active = true)\");\n\n// after\nem.createQuery(\"select new com.acme.OrderRef(o.id, o.code) from Order o \" +\n  \"where o.id in (select o2.id from Order o2 where o2.active = true)\");","handlingStrategy":"validation","validationCode":"// Fail fast at startup: parsing the query builds the plan and throws immediately\ntry (EntityManager em = emf.createEntityManager()) {\n    em.createQuery(\"select new com.acme.OrderRef(o.id, o.code) from Order o\", OrderRef.class);\n}\n// And guard the JPQL string: no 'select new' inside a parenthesized subquery\nString flat = jpql.toLowerCase().replaceAll(\"\\\\s+\", \" \");\nif (flat.matches(\"(?s).*\\\\(\\\\s*select\\\\s+new\\\\b.*\")) {\n    throw new IllegalArgumentException(\"dynamic instantiation inside a sub-query is unsupported\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    results = em.createQuery(jpql, Pair.class).getResultList();\n} catch (UnsupportedOperationException e) {\n    if (\"dynamic instantiation in a sub-query is unsupported\".equals(e.getMessage())) {\n        // rewrite: instantiate in the outer query, select plain values in the subquery\n    } else { throw e; }\n}","preventionTips":["Keep constructor expressions in the outermost SELECT only; subqueries select ids/scalars.","Declare projections as @NamedQuery so the plan is validated at bootstrap, not first use.","Add a query review rule: 'new ...' may not appear inside parentheses of a subquery."],"tags":["hibernate","orm","hql","jpql","subquery","dynamic-instantiation","unsupported-operation","query-projection"],"backgroundTag":"unsupported-subquery-constructor-instantiation","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}