{"record":{"id":"6391c86088eb2646","repo":"flowable/flowable-engine","slug":"query-return-results-size-results-instea-6391c8","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/AbstractNativeQuery.java","lineNumber":168,"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 maxResults\n     * @param firstResult\n     */\n    public abstract List<U> executeList(CommandContext commandContext, Map<String, Object> parameterMap, int firstResult, int maxResults);\n\n    public U executeSingleResult(CommandContext commandContext) {\n        List<U> results = executeList(commandContext, getParameterMap(), 0, Integer.MAX_VALUE);\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    private Map<String, Object> getParameterMap() {\n        HashMap<String, Object> parameterMap = new HashMap<>();\n        parameterMap.put(\"sql\", sqlStatement);\n        parameterMap.putAll(parameters);\n        return parameterMap;\n    }\n\n    public Map<String, Object> getParameters() {\n        return parameters;\n    }\n\n}\n","sourceCodeStart":150,"sourceCodeEnd":185,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/AbstractNativeQuery.java#L150-L185","documentation":"AbstractNativeQuery.executeSingleResult() backs nativeQuery().singleResult(). If executeList returns more than one row it throws plain ActivitiException \"Query return N results instead of max 1\", because singleResult() has a stricter contract: exactly one row expected. This is a query-contract violation, not an argument error.","triggerScenarios":"Calling nativeQuery().singleResult() where the native SQL matches multiple rows (e.g. missing unique filter like id = ? or an unprescribed where clause).","commonSituations":"Native queries written against tables with several matching records (multiple historic instances, versions, or active jobs); duplicated data in custom tables; missing LIMIT because singleResult was assumed to limit the query (it does not limit semantically — it fetches all and checks).","solutions":["Tighten the native query WHERE clause so it can match at most one row (filter by primary key/unique column).","Use list() / listPage(0, 1) instead of singleResult() when multiple matches are legitimate, and pick the first.","Add a deterministic ORDER BY + limiting filter if the newest/oldest row is wanted (e.g. RES_ID_ = ? or ORDER BY ... then listPage(0,1)).","Check data for unintended duplicates."],"exampleFix":"// before\nTask t = taskService.createNativeTaskQuery()\n    .sql(\"SELECT * FROM ACT_RU_TASK WHERE NAME_ = #{name}\")\n    .parameter(\"name\", name).singleResult(); // may match many\n// after\nList<Task> ts = taskService.createNativeTaskQuery()\n    .sql(\"SELECT * FROM ACT_RU_TASK WHERE NAME_ = #{name} ORDER BY CREATE_TIME_ DESC\")\n    .parameter(\"name\", name).listPage(0, 1);\nTask t = ts.isEmpty() ? null : ts.get(0);","handlingStrategy":"try-catch","validationCode":"// verify uniqueness first\nLong n = nativeQuery.sql(sameSql).count(); // or run list() and check size <= 1","typeGuard":null,"tryCatchPattern":"try { return query.singleResult(); } catch (ActivitiException e) { if (e.getMessage().contains(\"results instead of max 1\")) { return query.listPage(0,1).stream().findFirst().orElse(null); } throw e; }","preventionTips":["Only call singleResult() on queries filtering by primary/unique keys","Prefer list() plus size checks in application code","Add DB constraints/unique indexes on custom tables queried natively"],"tags":["query","native-query","single-result","database"],"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"}