{"record":{"id":"15243d590c484b78","repo":"flowable/flowable-engine","slug":"query-return-results-size-results-instea","errorCode":null,"errorMessage":"Query return \" + results.size() + \" results instead of max 1","messagePattern":"Query return \" \\+ results\\.size\\(\\) \\+ \" results instead of max 1","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/query/AbstractNativeQuery.java","lineNumber":133,"sourceCode":"        }\n    }\n\n    public abstract long executeCount(CommandContext commandContext, Map<String, Object> parameterMap);\n\n    /**\n     * Executes the actual query to retrieve the list of results.\n     *\n     * @param commandContext\n     * @param parameterMap\n     */\n    public abstract List<U> executeList(CommandContext commandContext, Map<String, Object> parameterMap);\n\n    public U executeSingleResult(CommandContext commandContext) {\n        List<U> results = executeList(commandContext, generateParameterMap());\n        if (results.size() == 1) {\n            return results.get(0);\n        } else if (results.size() > 1) {\n            throw new FlowableException(\"Query return \" + results.size() + \" results instead of max 1\");\n        }\n        return null;\n    }\n\n}\n","sourceCodeStart":115,"sourceCodeEnd":139,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/query/AbstractNativeQuery.java#L115-L139","documentation":"Result-shape guard in AbstractNativeQuery.executeSingleResult: singleResult() was called but the native query returned more than one row (count included in message); the caller must narrow the query or use list().","triggerScenarios":"Calling nativeQuery().singleResult() (or executeSingleResult) when the native SQL matches multiple rows — e.g. missing LIMIT 1 or a WHERE clause that isn't unique.","commonSituations":"Native SQL without LIMIT/TOP returned several matches; unique constraint assumptions broken by duplicate rows; developer used singleResult() assuming at most one row.","solutions":["Add a where-clause/limit so the native query returns exactly one row","Use list() instead of singleResult() when multiple rows are expected"],"exampleFix":"// before\nObject r = managementService.createTableQuery()\n    .sql(\"SELECT * FROM ACT_RU_EXECUTION WHERE PROC_INST_ID_ = #{pid}\")\n    .singleResult();\n// after\nObject r = managementService.createTableQuery()\n    .sql(\"SELECT * FROM ACT_RU_EXECUTION WHERE PROC_INST_ID_ = #{pid} LIMIT 1\")\n    .singleResult();","handlingStrategy":"validation","validationCode":"// ensure uniqueness before singleResult()\nList<Map<String,Object>> all = managementService.createTableQuery()\n    .sql(\"SELECT * FROM ACT_RU_EXECUTION WHERE PROC_INST_ID_ = #{pid}\").list();\nif (all.size() > 1) throw new IllegalStateException(\"expected at most 1, got \" + all.size());","typeGuard":null,"tryCatchPattern":"try {\n    return nativeQuery.singleResult();\n} catch (FlowableException e) {\n    if (e.getMessage().contains(\"instead of max 1\")) {\n        return nativeQuery.list().get(0);\n    }\n    throw e;\n}","preventionTips":["Add LIMIT 1 to native SQL used with singleResult()","Restrict WHERE clauses to unique keys","Prefer .list() when multiplicity is possible"],"tags":["query","native-query","multiple-results"],"backgroundTag":"unexpected-response-shape","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}