{"record":{"id":"843da0602b69c379","repo":"flowable/flowable-engine","slug":"query-return-results-size-results-instea-843da0","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":"ActivitiException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/AbstractQuery.java","lineNumber":194,"sourceCode":"        }\n    }\n\n    public abstract long executeCount(CommandContext commandContext);\n\n    /**\n     * Executes the actual query to retrieve the list of results.\n     * \n     * @param page\n     *            used if the results must be paged. If null, no paging will be applied.\n     */\n    public abstract List<U> executeList(CommandContext commandContext, Page page);\n\n    public U executeSingleResult(CommandContext commandContext) {\n        List<U> results = executeList(commandContext, null);\n        if (results.size() == 1) {\n            return results.get(0);\n        } else if (results.size() > 1) {\n            throw new ActivitiException(\"Query return \" + results.size() + \" results instead of max 1\");\n        }\n        return null;\n    }\n\n    protected void addOrder(String column, String sortOrder, NullHandlingOnOrder nullHandlingOnOrder) {\n\n        if (orderBy == null) {\n            orderBy = \"\";\n        } else {\n            orderBy = orderBy + \", \";\n        }\n\n        String defaultOrderByClause = column + \" \" + sortOrder;\n\n        if (nullHandlingOnOrder != null) {\n\n            if (nullHandlingOnOrder == NullHandlingOnOrder.NULLS_FIRST) {\n","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/AbstractQuery.java#L176-L212","documentation":"AbstractQuery.executeSingleResult() backs typedQuery.singleResult(). It runs executeList without paging and throws ActivitiException \"Query return N results instead of max 1\" when more than one row matches. Unlike SQL, the singleResult() contract does not silently take the first row — multiple matches are an error.","triggerScenarios":"Calling e.g. taskQuery.processDefinitionKey(key).singleResult() where the criteria match several tasks/instances/executions/variables.","commonSituations":"Queries keyed by non-unique attributes (process definition key instead of id, business key with multiple running instances, variable value shared by many rows); historical queries over long time ranges; deployments producing duplicate definitions.","solutions":["Add unique criteria (processInstanceId, taskId, executionId, processDefinitionId) before singleResult().","Use .list() and inspect size, or listPage(0,1) plus ORDER BY when multiple matches are valid.","Use latest().singleResult() variants (e.g. processDefinitionQuery.latestVersion()) where the API supports it.","Deduplicate data or narrow historic query time range."],"exampleFix":"// before\nProcessDefinition pd = repositoryService.createProcessDefinitionQuery()\n    .processDefinitionKey(\"order\").singleResult(); // multiple versions match\n// after\nProcessDefinition pd = repositoryService.createProcessDefinitionQuery()\n    .processDefinitionKey(\"order\").latestVersion().singleResult();","handlingStrategy":"try-catch","validationCode":"if (query.count() > 1) { /* narrow criteria or use list */ }","typeGuard":null,"tryCatchPattern":"try { return query.singleResult(); } catch (ActivitiException e) { if (e.getMessage().contains(\"results instead of max 1\")) { return query.listPage(0,1).get(0); } throw e; }","preventionTips":["Filter by unique ids (processInstanceId, taskId) before singleResult()","Use latestVersion()/latest() where supported","Audit queries keyed by business attributes for multi-match risk"],"tags":["query","single-result","database","api-contract"],"backgroundTag":"unexpected-result-count","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"}